一些很酷的.Net技巧

原做出處:http://www.codeproject.com/useritems/tips.asp?df=100html

一..Net Frameworkweb

1.  如何得到系統文件夾c#

使用System.Envioment類的GetFolderPath方法;例如:windows

Environment.GetFolderPath( Environment.SpecialFolder.Personal )app

2.  如何得到正在執行的exe文件的路徑函數

1)  使用Application類的ExecutablePath屬性工具

2)  System.Reflection.Assembly.GetExecutingAssembly().Locationpost

3.  如何檢測操做系統的版本字體

使用EnviomentOSVersion屬性,例如:this

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.  Vbc#的語法有什麼不一樣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#只是將引用賦值給了新的變量實質上是對同一個變量的引用,那麼咱們怎樣才能夠賦值的同時建立一個全新的變量而不僅是對實例引用的賦值呢?咱們可使用複製構造函數。

咱們能夠爲類創造一個只用一個類型爲該類型的參數的構造函數,如:

 

 

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不顯示標題欄?

經過設置formText屬性爲空字符串,設置ControlBox屬性爲false

form1.Text = string. Empty;

form1.ControlBox = false;

2.如何使winform的窗體使用XP的風格

見原做

3.如何禁止form在工具欄顯示

設置formShowInTaskbar屬性爲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).WidthHeight)屬性得到屏幕的大小,而後使用一個timer根據時間改變窗口的位置

五.Button控件

1.如何設置form的默認button(即在form上按下回車時觸發的button

         能夠設置formAcceptButton屬性:form1.AcceptButton = button1;

2. 如何設置form的取消button(即在用戶按下Esc鍵時觸發的button

         能夠設置formCancelButton屬性:form1.CancelButton = buttonC;

3. 如何經過程序觸發一個buttonClick事件

         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;

 

出處:http://www.cnblogs.com/yukaizhao/archive/2007/04/08/dotnet_tips_cool.html

相關文章
相關標籤/搜索