一些很酷的.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

相关文章
相关标签/搜索