顯示具有 C Sharp 標籤的文章。 顯示所有文章
顯示具有 C Sharp 標籤的文章。 顯示所有文章

星期四, 7月 28, 2011

[C#] HttpWebRequest request limitation

最近實作多執行緒上傳檔案的測試工具時,
發現不管執行幾個執行緒同時只會有二個連線數存在。
這是.net預設的連線數,透過以下設定即可。
ServicePointManager.DefaultConnectionLimit = 10;
網路上有人建議最大連線數不要超過1024,最好的效果是512。
另外也可以從app.config來設定最大連線數

星期五, 7月 15, 2011

[C#] Settings

To Write and Persist User Settings at Run Time
  1. Access the user setting and assign it a new value, as shown in the following example:
    Properties.Settings.Default.myColor = Color.AliceBlue;
    
  2. If you want to persist changes to user settings between application sessions, call the Savemethod, as shown in the following code:
    Properties.Settings.Default.Save();

星期四, 4月 14, 2011

md5 image file

string ImageFileName = null;
string StrImageName = null;
string UploadImageFolderPath = Server.MapPath("../Upload/Images/");
string ImageFileExtension;

if (ImageUpload.HasFile)
{
StrImageName = ImageUpload.FileName.ToString();
ImageFileExtension = System.IO.Path.GetExtension(StrImageName);

// GIVE A UNIQUE FILENAME(MD5) TO THE UPLOADED FILE:

MD5CryptoServiceProvider objMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(StrImageName);
bs = objMD5.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();

foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}

StrImageName = s.ToString();

ImageFileName = StrImageName + ImageFileExtension;

// As long as the Image-File-Name is exists in the folder then we are MD5ing again the imgage-file-name:

while (System.IO.File.Exists(UploadImageFolderPath + ImageFileName))
{
MD5CryptoServiceProvider objMD5While = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bsWhile = System.Text.Encoding.UTF8.GetBytes(StrImageName);
bsWhile = objMD5.ComputeHash(bsWhile);
System.Text.StringBuilder sWhile = new System.Text.StringBuilder();

foreach (byte bWhile in bsWhile)
{
sWhile.Append(bWhile.ToString("x2").ToLower());
}

StrImageName = sWhile.ToString();
ImageFileName = StrImageName + ImageFileExtension;
}

ImageUpload.PostedFile.SaveAs(UploadImageFolderPath + ImageFileName);
}

星期三, 4月 13, 2011

圖片檔案hash

string hash = null;
                using (System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider())
                {
                    System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    System.Drawing.Image myImage = System.Drawing.Image.FromFile(path + f.ToString());
                    myImage.Save(ms, format);

                    hash = Convert.ToBase64String(sha1.ComputeHash(ms.ToArray()));
                }

星期日, 1月 09, 2011

[Asp.Net] Https using WebClient

等入驗證之前先補上這一段就可以取得加密的憑證

即可用正常的webclient
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback =
delegate(object senderX, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
HtmlText = wc.DownloadString(wc.BaseAddress);

}
catch
{
System.Environment.Exit(System.Environment.ExitCode);
}

星期一, 11月 22, 2010

WebDAV (Web Distributed Authoring and Versioning) using .Net C#

WebDAV(Web Distributed Authoring and Versioning)為一個HTTP/1.1通訊協定的延伸建議RFC-2518,讓用戶端使用者可以透過網路來進行網頁內容的編輯工作。
WebDAV透過HTTP/1.1通訊協定標準,提供簡單的檔案輸入/輸出(simple File I/O)的功能,包含:
  • 建立、修改、刪除檔案及瀏覽目錄和檔案。
  • 讀取檔案與目錄的屬性(properties)。
  • 簡單的檔案鎖定。
WebDAV提供幾個新的HTTP指令,包含:
  • GET:讀取WebDAV目錄下的檔案內容。
  •  PUTPOST:傳送檔案內容到伺服器WebDAV目錄下。
  •  PROPFINDPROPPATCH:讀取、設定檔案屬性。
  • COPYMOVECOPY複製目錄、或檔案,MOVE移動檔案。限於同一個WebDAV目錄操作。COPY時若無此路逕時會自動產生,COPY複製範圍也包括其子目錄。
  • MKCOL:建立一個目錄。
  • DELETE:刪除一個檔案或目錄。
  • LOCKUNLOCK:鎖定檔案、解除檔案鎖定。
  • SEARCH:使用SQL語法搜查檔案內容,可使用全文檢索,僅適用於Exchange 2000 Server共用資料匣。 
安裝Windows 2000IE 5、或Office 2000的用戶端電腦,具有權限的使用者,就可以針對IIS的虛擬目錄來發行、鎖定、管理Web的資源,將文件發行至Web伺服器,及在Web目錄中處理檔案,包含:
  • 移動、複製檔案:擁具權限的使用者可以在WebDAV目錄中移動、複製檔案。
  • 修改檔案:擁具權限的使用者可以讀取、修改寫入檔案的內容。
  •  鎖定檔案:多位使用者可以同時讀取同一個檔案,讀取時會將檔案鎖定,因此同時只有一人可以修改同一個檔案。
  • 搜尋檔案:連線到WebDAV目錄後,就可以搜尋WebDAV目錄中的檔案與內容,譬如搜尋到所有由Jack所建立的檔案,或者搜尋所有含有IIS關鍵字的檔案。



Reference:
WebDAV .NET
WebDav and Outlook Appointments in .NET
網際網路新通訊協定---WebDAV

星期日, 11月 14, 2010

[C# WinForm]Notifyicon with contextmenu and no form

實作沒有表單的contextMenu
using System;using System.Windows.Forms;
using System.Threading;using System.Drawing;
namespace WindowsApplication9
{   static class Program   {
      [STAThread]      static void Main()
      {        
         Application.EnableVisualStyles();         
         Application.SetCompatibleTextRenderingDefault(false);         
         NotifyIcon notifyIcon1 = new NotifyIcon();                
         ContextMenu contextMenu1 = new ContextMenu();        
         MenuItem menuItem1 = new MenuItem();
         contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1 });
         menuItem1.Index = 0;
         menuItem1.Text = "E&xit";
         menuItem1.Click += new EventHandler(menuItem1_Click);
         notifyIcon1.Icon = new Icon("app.ico");
         notifyIcon1.Text = "Form1 (NotifyIcon example)";
         notifyIcon1.ContextMenu = contextMenu1;
         notifyIcon1.Visible = true;         Application.Run();
         notifyIcon1.Visible = false;
      }
      private static void menuItem1_Click(object Sender, EventArgs e)
      {         Application.Exit();
      }
   }
}
Reference: Notifyicon with contextmenu and no form

星期日, 11月 07, 2010

[C# WinForm]在程式中開啟 檔案總管

    EXPLORER.EXE [/n][/e][,/root,][[,/select],

    /n:會針對每一個選取的項目,以單窗格 (我的電腦) 檢視方式開啟一個新視窗,
    即使新視窗與已開啟的視窗重複
    也一樣。
    
    /e:會使用 Windows 檔案總管檢視。Windows 檔案總管檢視十分類似
    Windows 3.x 版中的檔案管理員。請注意,預設檢視為
    開啟檢視。
    
    /root,:會指定所指定之檢視的根層級。預設 是使用標準命名空間根目錄 ( 桌面)。所指定的就是顯示器的根目錄 。  /select,:會指定成為初始焦點的資料夾 。如果使用 "/select",則會開啟
    上層資料夾,並選取所指定的物件。
    • "My Computer" highlighted in left side with all drives visible but not expanded and C: highlighted in right side: %SystemRoot%explorer.exe /e,/select,c:
    • Desktop highlighted and nothing expanded: %SystemRoot%explorer.exe /e,/n,/select,/root,c:
    • All drives visible and the system drive highlighted and expanded in full screen: %SystemRoot%explorer.exe /e,/select
    • All drives visible and the system drive expanded in small screen: %SystemRoot%explorer.exe /e,/select,%systemroot%
    • Only Windows Directory visible highlighted and expanded: %SystemRoot%explorer.exe /e,/root,%systemroot%
    • All drives visible but only C: highlighted and expanded: %SystemRoot%explorer.exe /e,c:
    • Nothing expanded and My Computer highlighted in right side: %SystemRoot%explorer.exe /n,/e,/select,
    • Opens the Windows folder as a folder: %SystemRoot%explorer.exe %systemroot%
    • Opens as "My Computer": %SystemRoot%explorer.exe %systemroot%,
    • This opens the Desktop folder with "My Computer" highlighted: %SystemRoot%explorer.exe %systemroot%,/select,
    • "Desktop" highlighted in the left side and no drives visible:
      %systemroot%explorer.exe /e,/root,::{20D04FE0-3AEA-1069-A2D8-08002B30309D},/select
    • "My Computer" highlighted in left side and all drives visible but none expanded:
      %systemroot%explorer.exe /e,/root,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
    • "Desktop" in left side highlighted and "My Computer" highlighted in right side and no drives visible:
      %systemroot%explorer.exe /e,/select,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}

    Reference:
    在程式中開啟 檔案總管 (Using C#)
    如何開啟檔案總管在想要的目錄.

    星期日, 9月 26, 2010

    星期四, 9月 23, 2010

    [C# Winform] Socket Survey


    根據MSDN所寫:
    執行緒不是背景執行緒就是前景執行緒。
    背景執行緒和前景執行緒相同,唯一差別在於背景執行緒不會防止處理序終止。
    一旦終止屬於處理序的所有前景執行緒之 後,
    Common Language Runtime 就會結束處理序。
    剩下的所有背景執行緒則會在尚未完成之前全部停止

    C# Multi threaded socket programming 
    The C# Multi Threaded Socket Program has two sections.
    Reference:
    C# WinForm Socket

    星期六, 8月 07, 2010

    星期一, 3月 01, 2010

    C# 設定系統時間



    [StructLayout(LayoutKind.Sequential)]
    public struct SystemTime
    {
    public ushort wYear;
    public ushort wMonth;
    public ushort wDayOfWeek;
    public ushort wDay;
    public ushort wHour;
    public ushort wMinute;
    public ushort wSecond;
    public ushort wMiliseconds;
    }
    public class SetSystemDateTime
    {
    [DllImport("Kernel32.dll")]
    public static extern bool SetLocalTime(ref SystemTime sysTime);

    public static bool SetLocalTimeByStr(string timestr)
    {
    bool flag = false;
    SystemTime sysTime = new SystemTime();
    DateTime dt = Convert.ToDateTime(timestr);
    sysTime.wYear = Convert.ToUInt16(dt.Year);
    sysTime.wMonth = Convert.ToUInt16(dt.Month);
    sysTime.wDay = Convert.ToUInt16(dt.Day);
    sysTime.wHour = Convert.ToUInt16(dt.Hour);
    sysTime.wMinute = Convert.ToUInt16(dt.Minute);
    sysTime.wSecond = Convert.ToUInt16(dt.Second);
    try
    {
    flag = SetSystemDateTime.SetLocalTime(ref sysTime);
    }
    catch (Exception e)
    {
    Console.WriteLine("SetSystemDateTime函数执行异常" + e.Message);
    }
    return flag;
    }
    }

    星期四, 1月 14, 2010

    C# StreamReader 逐行讀取txt file資料

    今天又用到了XD,記錄一下。

      
    using(StreamReader sr = new StreamReader(ckipPath,Encoding.Default))
    {
    while ((termTemp = sr.ReadLine()) != null)
    {
    termTemp = termTemp.Substring(0,termTemp.IndexOf(" "));
    }
    }
    
    

    星期三, 1月 13, 2010

    C# Base64 Convert

    /// 
    /// base64convert 的摘要描述
    /// 
    public class base64convert
    { 
    
        public base64convert()
        {
            
        }
    
        public string ConvertStringToBase64(string s)
        {
            byte[] ToByte = System.Text.Encoding.Default.GetBytes(s.ToString().Trim());
    
            string str = Convert.ToBase64String(ToByte);
    
            return str;
        }
    
        public string ConvertBase64ToString(string s)
        {
            byte[] strbyte = Convert.FromBase64String(s.ToString().Trim());
            string str = System.Text.Encoding.Default.GetString(strbyte);
    
            return str;
        }
    }
    
    

    其他你感興趣的文章

    Related Posts with Thumbnails