一..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#只是將引用賦值給了新的變數實質上是對同一個變數的引用,那麼我們怎樣才可以賦值的同時建立一個全新的變數而不只是對執行個體引用的賦值呢?我們可以使用複製建構函式。
我們可以為類創造一個只用一個類型為該類型的參數的建構函式,如:
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;