遇到Asp.net無法上傳大檔案嗎?
只需要修改Web.config,即可上傳大檔案喔!
<system.web> <!-- 最大上傳檔案大小100MB(100*1024) TimeOut時間300秒 --> <httpRuntime maxRequestLength="102400" executionTimeout="300"/> </system.web>
C#可以使用Drawing處理圖片,但儲存時預設壓縮品質較差,使用下面方法可以自訂壓縮比率。
System.Drawing.Image image = System.Drawing.Image.FromFile(@"C:\a.png"); EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, Convert.ToInt64(60)); //品質60 image.Save(@"C:\b.png", GetEncoder(ImageFormat.Png), myEncoderParameters); //儲存成png image.Dispose(); private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) return codec; } return null; }
假如有一張小圖要嵌入頁面,會用下列語法: <img src="xxx.jpg">
如果使用 Data URI 就會變為: <img src="data:image/jpg;base64,..................">
當頁面為 html + img 就等於需要兩次 Request,如果改用 Data URI 僅需傳送 html 一次 Request,所以可以達到載入加快的效果。
※但如果圖片有快取,第二次載入反而不會比較快,使用上請自行斟酌。
C#直接將圖片載入成 Data URI:
protected string GetDataURL(string imgFile) { return "<img src=\"data:image/" + Path.GetExtension(imgFile).Substring(1) + ";base64," + Convert.ToBase64String(File.ReadAllBytes(imgFile)) + "\" />"; }
頁面上只需將img改為
<%=GetDataURL(Server.MapPath("~/temp.jpg"))%>
以往讀取檔案使用 ReadAllLines,需要一次載入所有內容,所以會有卡住的情況發生。
foreach (string line in File.ReadAllLines(@"C:\a.log")) { Console.WriteLine(line); }
為了要改善,所以改寫為以下,一次讀取一行:
using (StreamReader SR = new StreamReader(@"C:\a.log")) { string line; while ((line = SR.ReadLine()) != null) { Console.WriteLine(line); } }
列出指定目錄的所有檔案:
foreach (string route in Directory.GetFiles(@"C:\")) { Console.WriteLine(route); }
列出指定目錄的所有資料夾:
foreach (string route in Directory.GetDirectories(@"C:\")) { Console.WriteLine(route); }
C#開發的封包監測軟體
官方網站下載:http://www.mentalis.org/soft/projects/pmon/
本地下載:PacketMonitor
以往常用組合方式改變網址結構:
string Url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.RawUrl;
利用 .Net 的 UriBuilder 修改網址結構:
string Url = new UriBuilder("http://www.cscworm.net/") { Scheme = Uri.UriSchemeHttps, Port = 443 }.ToString(); string Url = new UriBuilder(Request.Url) { Scheme = Uri.UriSchemeHttps, Port = 443 }.ToString();
Thread並沒有內建超時自動結束的功能,所以利用Join等待的特性,再利用Abort來結束Thread。
using System; using System.Threading; namespace Test { class Program { static void Main(string[] args) { Thread t = new Thread(new ThreadStart(delegate() { Console.WriteLine("Thread開始"); Thread.Sleep(3000); //等待3秒 Console.WriteLine("Thread結束"); })); t.Start(); Console.WriteLine("Thread等待"); if (!t.Join(2000)) t.Abort(); //超過兩秒結束Thread Console.WriteLine("程式結束"); Console.ReadLine(); } } }
首先下載 mongodb-csharp
原始網址:https://github.com/samus/mongodb-csharp/downloads
本站下載:MongoDBDriver-Release-0.90.0-Beta-1
using System; using System.Linq; namespace MongoDB { class Program { static void Main(string[] args) { var mongo = new Mongo("mongodb://192.168.3.129"); mongo.Connect(); var DB = mongo.GetDatabase("TEST_DB"); var TABLE = DB.GetCollection("TEST_TABLE"); /*新增資料行*/ { var DOC = new Document(); DOC["Name"] = "Tom"; DOC["Sex"] = true; DOC["Year"] = 18; TABLE.Insert(DOC); } /*列出指定TABLE資料列*/ { foreach (var DOC in TABLE.FindAll().Documents) { Console.WriteLine(DOC["Name"]); Console.WriteLine(DOC["Sex"]); Console.WriteLine(DOC["Year"]); } } /*查詢指定資料*/ { var DOC = new Document(); DOC["Name"] = "Tom"; var category = TABLE.FindOne(DOC); Console.WriteLine(category["Name"]); Console.WriteLine(category["Sex"]); Console.WriteLine(category["Year"]); } //MongoDB默認的索引_id(類似DB的主鍵)索引名稱叫做_id_ 列出索引資訊 var _idindex = TABLE.MetaData.Indexes.Single(s => s.Key == "_id_"); Console.WriteLine(_idindex); //將TABLE中的Sex欄位建立成為 單一索引(1正排列 -1反排列) TABLE.MetaData.CreateIndex(new Document() { { "Sex", 1 } }, false); //將TABLE中的Sex欄位建立成為 多重索引 TABLE.MetaData.CreateIndex(new Document() { { "Name", 1 }, { "Sex", -1 } }, false); //將TABLE中的Name欄位建立成為 唯一索引 TABLE.MetaData.CreateIndex(new Document() { { "Name", 1 } }, true); /*列出TABLE的所有索引*/ foreach (var index in TABLE.MetaData.Indexes) { Console.WriteLine(index.Value); } //刪除TABLE的索引 TABLE.MetaData.DropIndex("_Sex_"); } } }
測試程式碼如下:
using System; using System.Diagnostics; namespace WebApplication { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string temp = string.Empty; Stopwatch sw = new Stopwatch(); //HttpRuntime.Cache 測試 sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) SetCache("cache" + i.ToString(), i.ToString(), 60); sw.Stop(); Response.Write("HttpRuntime.Cache 寫入時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>"); sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) temp = GetCache("cache" + i.ToString()); sw.Stop(); Response.Write("HttpRuntime.Cache 讀取時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br><br>"); //Page.Application 測試 sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) Application["cache" + i.ToString()] = i.ToString(); sw.Stop(); Response.Write("Page.Application 寫入時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>"); sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) temp = Application["cache" + i.ToString()].ToString(); sw.Stop(); Response.Write("Page.Application 讀取時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>"); } /// <summary> /// 取的快取 /// </summary> /// <param name="key"></param> /// <returns></returns> protected string GetCache(string key) { try { object _cache = System.Web.HttpRuntime.Cache.Get(key); if (_cache != null) return (string)_cache; else return ""; } catch { return ""; } } /// <summary> /// 設定快取 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="timeout"></param> /// <returns></returns> protected bool SetCache(string key, string value,int timeout) { try { System.Web.HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, timeout), System.Web.Caching.CacheItemPriority.High, null); return true; } catch { return false; } } } }