一些很酷的.Net技巧

来源:互联网 发布:javascript实战 编辑:程序博客网 时间:2024/06/08 09:49

原作出處:http://www.codeproject.com/useritems/tips.asp?df=100
  一..Net Framework
  1. 如何獲得系統文件夾
  使用System.Envioment類的GetFolderPath方法;例如:
  Environment.GetFolderPath( Environment.SpecialFolder.Personal )
  2. 如何獲得正在執行的exe文件的路徑
  1) 使用Application類的ExecutablePath屬性
  2) System.Reflection.Assembly.GetExecutingAssembly().Location
  3. 如何檢測操作系統的版本
  使用Envioment的OSVersion屬性,例如:
  OperatingSystem os = Environment.OSVersion;
  MessageBox.Show(os.Version.ToString());
  MessageBox.Show(os.Platform.ToString());
  4. 如何根據完整的文件名獲得文件的文件名部分、
  使用System.IO.Path類的方法GetFileName或者GetFileNameWithoutExtension方法
  5. 如何通過文件的全名獲得文件的擴展名
  使用System.IO.Path.GetExtension靜態方法
  6. Vb和c#的語法有什麽不同click here
  7. 如何獲得當前電腦用戶名,是否聯網,幾個顯示器,所在域,鼠標有幾個鍵等信息
  使用System.Windows.Forms. SystemInformation類的靜態屬性
  8. 修飾Main方法的[STAThread]特性有什麽作用
  標示當前程序使用單線程的方式運行
  9. 如何讀取csv文件的內容
  通過OdbcConnection可以創建一個鏈接到csv文件的鏈接,鏈接字符串的格式是:"Driver={Microsoft Text Driver (*.txt;*.csv)};Dbq="+cvs文件的文件夾路徑+" Extensions=asc,csv,tab,txt; Persist Security Info=False";
  創建連接之後就可以使用DataAdapter等存取csv文件了。
  詳細信息見此處
  10. 如何獲得磁盤開銷信息,代碼片斷如下,主要是調用kernel32.dll中的GetDiskFreeSpaceEx外部方法。
  public sealed class DriveInfo
  {
   [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")]
   private static extern long GetDiskFreeSpaceEx(string lpDirectoryName,
   out long lpFreeBytesAvailableToCaller,
   out long lpTotalNumberOfBytes,
   out long lpTotalNumberOfFreeBytes);
   public static long GetInfo(string drive, out long available, out long total, out long free)
   {
   return GetDiskFreeSpaceEx(drive, out available, out total, out free);
   }
   public static DriveInfoSystem GetInfo(string drive)
   {
   long result, available, total, free;
   result = GetDiskFreeSpaceEx(drive, out available, out total, out free);
   return new DriveInfoSystem(drive, result, available, total, free);
   }
  }
  public struct DriveInfoSystem
  {
   public readonly string Drive;
   public readonly long Result;
   public readonly long Available;
   public readonly long Total;
   public readonly long Free;
   public DriveInfoSystem(string drive, long result, long available, long total, long free)
   {
   this.Drive = drive;
   this.Result = result;
   this.Available = available;
   this.Total = total;
   this.Free = free;
   }
  }
  可以通過
  DriveInfoSystem info = DriveInfo.GetInfo("c:");來獲得指定磁盤的開銷情況
  11.如何獲得不區分大小寫的子字符串的索引位置
   1)通過將兩個字符串轉換成小寫之後使用字符串的IndexOf方法:
  string strParent = "The Codeproject site is very informative.";
  string strChild = "codeproject";
  // The line below will return -1 when expected is 4.
  int i = strParent.IndexOf(strChild);
  // The line below will return proper index
  int j = strParent.ToLower().IndexOf(strChild.ToLower());
   2)
  一種更優雅的方法是使用System.Globalization命名空間下面的CompareInfo類的IndexOf方法:
  using System.Globalization;
  string strParent = "The Codeproject site is very informative.";
  string strChild = "codeproject";
  // We create a object of CompareInfo class for a neutral culture or a culture insensitive object
  CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
  int i = Compare.IndexOf(strParent,strChild,CompareOptions.IgnoreCase);
  二
  . OOPs
  1. 什麽是複制構造函數
   我們知道構造函數是用來初始化我們要創建實例的特殊的方法。通常我們要將一個實例賦值給另外一個變量c#只是將引用賦值給了新的變量實質上是對同一個變量的引用,那麽我們怎樣才可以賦值的同時創建一個全新的變量而不只是對實例引用的賦值呢?我們可以使用複制構造函數。
  我們可以爲類創造一個只用一個類型爲該類型的參數的構造函數,如:
http://www.knowsky.com/
  public Student(Student student)
  {
  this.name = student.name;
  }
  使用上面的構造函數我們就可以複制一份新的實例值,而非賦值同一引用的實例了。
  class Student
  {
   private string name;
   public Student(string name)
   {
   this.name = name;
   }
   public Student(Student student)
   {
   this.name = student.name;
   }
   public string Name
   {
   get
   {
   return name;
   }
   set
   {
   name = value;
   }
   }
  }
  class Final
  {
   static void Main()
   {
   Student student = new Student ("A");
   Student NewStudent = new Student (student);
   student.Name = "B";
   System.Console.WriteLine("The new student's name is {0}", NewStudent.Name);
   }
  }
  The new student's name is A.
  2.什麽是只讀常量
  就是靜態的只讀變量,它通常在靜態構造函數中賦值。
  class Numbers
  {
   public readonly int m;
   public static readonly int n;
   public Numbers (int x)
   {
   m=x;
   }
   static Numbers ()
   {
   n=100;
   }
  } //其中n就是一個只讀的常量,對于該類的所有實例他只有一種值,而m則根據實例不同而不同
  三.VS.Net IDE
  1. 2請看原作
  3.如何改變region的顔色
   通過工具 à 選項 à 環境 à 字體和顔色 à 可折疊文本設置
  四.WinForm
  1.如何使winForm不顯示標題欄?
  通過設置form的Text屬性爲空字符串,設置ControlBox屬性爲false
  form1.Text = string. Empty;
  form1.ControlBox = false;
  2.如何使winform的窗體使用XP的風格
  見原作
  3.如何禁止form在工具欄顯示
  設置form的ShowInTaskbar屬性爲false即可
  4.如何使程序打開默認的郵件程序並帶有一些參數讓用戶開始寫郵件
   1)如果是web程序:
   <a href=”mailto:email@address1.com,email@address2.com?cc=email@address3.com&Subject=Hello&body=Happy New Year”>some text</a>
   2) 對于windows程序,需要使用System.Diagnostics.Process類
  Process process = new Process();
  process.StartInfo.FileName = "mailto:email@address1.com,email@address2.com?subject=Hello&cc=email@address3.com
&bcc=email@address4.com&body=Happy New Year" ;
  process.Start();
  5.如何創建類似msn提示窗口
  需要獲得通過Screen.GetWorkingArea(this).Width(Height)屬性獲得屏幕的大小,然後使用一個timer根據時間改變窗口的位置
  五.Button控件
  1.如何設置form的默認button(即在form上按下回車時觸發的button)
   可以設置form的AcceptButton屬性:form1.AcceptButton = button1;
  2. 如何設置form的取消button(即在用戶按下Esc鍵時觸發的button)
   可以設置form的CancelButton屬性:form1.CancelButton = buttonC;
  3. 如何通過程序觸發一個button的Click事件
   Button1.PerformClick
  六.Combo Box
  1.如何使用可選字體填充Combo Box
  comboBox1.Items.AddRange (FontFamily.Families);
  七.TextBox
  1.如何禁用TextBox的默認上下文菜單(右鍵菜單)
  textBox1.ContextMenu = new ContextMenu();
  2,3 見原作
  4.如何在TextBox獲得焦點的時候,將焦點放在textBox文字的最後
  textBox1.SelectionStart = textBox1.Text.Length;(王朝網絡 wangchao.net.cn)

原创粉丝点击