十年

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
        本文假設讀者已有一定的編程基礎。
        要對目錄進行操作,須在程序中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");
            }

        }

        同樣,刪除目錄代碼如下:
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");
                }

            }

        }

        其他有關方法如下:

Rename or move a directory.

System.IO.Directory.Move

System.IO.DirectoryInfo.MoveTo

Delete a directory.

System.IO.Directory.Delete

System.IO.DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

Create a subdirectory.

CreateSubdirectory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

See all the files in all subdirectories of a directory.

GetFileSystemInfos

Find the size of a directory.

Directory

Determine if a file exists.

Exists

Sort files in a directory by size.

GetFileSystemInfos

Determine if a directory exists.

Exists


posted on 2005-01-06 09:38  留不住的时光  阅读(954)  评论(5编辑  收藏  举报