本文假設讀者已有一定的編程基礎。
要對目錄進行操作,須在程序中using System.IO,否則編譯無法通過。
程序界面如下:
要創建一個新目錄,在Dir Path文字框內輸入目錄路徑和名稱(如上圖紅字處),然後按New Dir按鈕,代碼如下:
同樣,刪除目錄代碼如下:
其他有關方法如下:
要對目錄進行操作,須在程序中using System.IO,否則編譯無法通過。
程序界面如下:
要創建一個新目錄,在Dir Path文字框內輸入目錄路徑和名稱(如上圖紅字處),然後按New Dir按鈕,代碼如下:
private void NewButton_Click(object sender, EventArgs e)
{
try
{
if (Directory.Exists(dirtext.Text))
{
MessageBox.Show("The Directory is exists.");
}
else
{
Directory.CreateDirectory(dirtext.Text);
MessageBox.Show("Create New Directory successfully");
}
}
catch
{
MessageBox.Show("Create Directory failed");
}
}
{
try
{
if (Directory.Exists(dirtext.Text))
{
MessageBox.Show("The Directory is exists.");
}
else
{
Directory.CreateDirectory(dirtext.Text);
MessageBox.Show("Create New Directory successfully");
}
}
catch
{
MessageBox.Show("Create Directory failed");
}
}
同樣,刪除目錄代碼如下:
private void DelButton_Click(object sender, EventArgs e)
{
if (!Directory.Exists(dirtext.Text))
{
MessageBox.Show("The Directory not exists");
return;
}
else
{
try
{
Directory.Delete(dirtext.Text);
MessageBox.Show("Successfully");
}
catch
{
MessageBox.Show("Failed");
}
}
}
{
if (!Directory.Exists(dirtext.Text))
{
MessageBox.Show("The Directory not exists");
return;
}
else
{
try
{
Directory.Delete(dirtext.Text);
MessageBox.Show("Successfully");
}
catch
{
MessageBox.Show("Failed");
}
}
}
其他有關方法如下:
Rename or move a directory. |
|
Delete a directory. |
|
Create a directory. |
|
Create a subdirectory. |
|
See the files in a directory. |
|
See the subdirectories of a directory. |
|
See all the files in all subdirectories of a directory. |
|
Find the size of a directory. |
Directory |
Determine if a file exists. |
|
Sort files in a directory by size. |
GetFileSystemInfos |
Determine if a directory exists. |