原作出处: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外部方法。
{
[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 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方法:
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);
二
. OOPs1. 什么是复制构造函数
我们知道构造函数是用来初始化我们要创建实例的特殊的方法。通常我们要将一个实例赋值给另外一个变量c#只是将引用赋值给了新的变量实质上是对同一个变量的引用,那么我们怎样才可以赋值的同时创建一个全新的变量而不只是对实例引用的赋值呢?我们可以使用复制构造函数。
我们可以为类创造一个只用一个类型为该类型的参数的构造函数,如:
{
this.name = student.name;
}
使用上面的构造函数我们就可以复制一份新的实例值,而非赋值同一引用的实例了。
{
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.什么是只读常量
就是静态的只读变量,它通常在静态构造函数中赋值。
{
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.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
需要获得通过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;
1. 在提交页面之后,保持滚动条的位置
可以在page指令上加上MaintainScrollPositionOnPostback指令
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="..." Inherits="..." %>
2. 在页面载入完之后,将焦点移动到某个控件,只需要指定Form的DefaultFocus属性就可以了。
<form id="frm" DefaultFocus="txtUserName" runat="server">
...
</form>
3. 通过DefaultButton属性设置form的默认相应按钮,即在用户敲回车时触发的按钮
<form id="frm" DefaultButton="btnSubmit" runat="server">
...
</form>
4. 可以使用$符号轻松的使用FindControl方法找到控件
<form id="form1" runat="server" DefaultFocus="formVw$txtName">
<div>
<asp:FormView ID="formVw" runat="server">
<ItemTemplate>
Name:
<asp:TextBox ID="txtName" runat="server"
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />
</ItemTemplate>
</asp:FormView>
</div>
</form>
在上面的例子中使用form的DefaultFocus属性指定页面载入时焦点所在的控件,使用$符号就可以轻松的定位txtName
也可以使用以下代码来轻松的找到控件
TextBox tb = this.FindControl("form1$formVw$txtName") as TextBox;
if (tb != null)
{
//Access TextBox control
}
5. 关于跨页提交的取得发出提交页面控件强类型的方法,见原文
6. 使用强类型访问MasterPage属性成员的方法,见原文
7. 我们可以使用验证控件的属性 ValidationGroup 指定该验证控件所属组,同时在button的ValidationGroup属性中指定该button所需要激活的验证组。
<form id="form1" runat="server">
Search Text: <asp:TextBox ID="txtSearch" runat="server" />
<asp:RequiredFieldValidator ID="valSearch" runat="Server"
ControlToValidate="txtSearch" ValidationGroup="SearchGroup" />
<asp:Button ID="btnSearch" runat="server" Text="Search"
ValidationGroup="SearchGroup" />
....
Other controls with validators and buttons defined here
</form>
1. 在开发web控件时,如果控件必须放在服务器端的form内,可以通过Page.VerifyRenderingInServerForm(Control) 方法来保证。
2. 使用Control类的ResolveClientUrl(string)方法可以将类似“~/abc/ab.aspx”这样的路径转换为正确的url路径,这在.Net1.0中是一个内部方法,而在2.0中是公开的方法
3. Button控件有两个事件,分别是OnClick和OnCommand,在点击button之后这两个事件都会被触发,两者的区别是后者可以接受commanArgs参数,而前者不可以。
4. 在写Web控件时可以通过Themable特性指定某属性是否可以在Skin文件中指定值。
5. .Net2.0的验证控件多了一个SetFocusOnError属性,可以指定发生错误的时候是否让焦点移动到要验证的控件上。
6. 在Aspx页面上可以使用<%$AppSettings: settingKey%>获得配置文件appSettings配置节中指定key的值
7. 当将textbox控件的ReadOnly属性设置为true时,如果在客户端用js脚本改变了这个textbox的值,提交之后在服务器端是得不到改动后的值的,可以通过Request.Form[textbox.ClientID]获得改动後的值。
8. 在.Net1.0中只有一个html input的上传文件控件,而在2.0中有了一个WebControl:FileUpload,在验证用户时候选择文件并上传时,可以通过它的HasFile属性来判断,而不必这样来判断FileUpload1.PostFile != null && FileUpload1.PostFile.ContentLength > 0。