国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#操作目錄與文件的方法步驟

2020-01-24 03:15:29
字體:
來源:轉載
供稿:網友
• 創建目錄和文件
1、通過Path類的Combine方法可以合并路徑。
復制代碼 代碼如下:

string activeDir = @"C:/myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");

2、目錄的創建。
創建目錄時如果目錄已存在,則不會重新創建目錄,且不會報錯。創建目錄時會自動創建路徑中各級不存在的目錄。
(1)通過Directory類的CreateDirectory方法創建。
復制代碼 代碼如下:

string activeDir = @"C:/myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);

(2)通過DirectoryInfo的對象創建。
復制代碼 代碼如下:

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:/myDirTwo/mySubDirThree");
di.Create();

3、文件的創建。
通過Create方法創建文件,會覆蓋同名的現有文件。創建文件時,該文件所在路徑的目錄必須存在,否則報錯。
(1)通過File類的Create方法創建。
復制代碼 代碼如下:

string activeDir = @"C:/myDir";
            string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
            System.IO.Directory.CreateDirectory(newPath);
            //創建一個空白文件
            string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
            System.IO.File.Create(filePathOne);

(2)通過FileInfo對象創建。
復制代碼 代碼如下:

//通過Combine合并目錄
            //然后創建目錄
            string activeDir = @"C:/myDir";
            string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
            System.IO.Directory.CreateDirectory(newPath);
            //創建一個空白文件
            string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
            System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
            fi.Create();

• 復制目錄文件
復制代碼 代碼如下:

//復制單個文件到指定目錄
            string fileName = "test.txt";
            string sourcePath = @"C:/testDir/subTestDir";
            string targetPath = @"C:/testDir/subTestDirTwo";
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
            if (!System.IO.Directory.Exists(targetPath))
                System.IO.Directory.CreateDirectory(targetPath);
            //如果已存在,參數為false時將報錯,參數為true重寫該文件
            //當copy方法為兩個參數時,默認重寫為false。
            System.IO.File.Copy(sourceFile, destFile, true);
            //以下為復制一個目錄下所有文件到指定目錄
            //如果復制有子目錄的目錄的所有文件,可以用遞歸或堆棧算法實現
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
                foreach (string s in files)
                {
                    //僅返回路徑字符串的文件名及后綴
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
        }

• 移動目錄和文件
復制代碼 代碼如下:

/*移動文件*/
            string sourceFile = @"C:/testDir/subTestDir/test.txt";
            string destFile = @"C:/testDir/subTestDirTwo/test.txt";
            //當目標文件存在時,拋出異常
            System.IO.File.Move(sourceFile, destFile);
            /*移動目錄*/
            //移動目錄將移動改目錄的子目錄和文件
            System.IO.Directory.Move(@"C:/testDir/subTestDirTwo/", @"C:/testDir/subTestDir");

• 刪除目錄和文件
1、刪除目錄
刪除目錄,如果該目錄不存在,會拋出異常。可以通過File類的Delete方法刪除目錄,也可以通過FileInfo對象方法刪除目錄。
(1)通過 File類的Delete方法刪除目錄
復制代碼 代碼如下:

//刪除可寫空目錄
            //如果不為空拋出目錄不為空異常
            try
            {
                System.IO.Directory.Delete(@"C:/testDir/subTestDir");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
            //第二參數為false時,只能刪除空目錄,否則拋出不為空異常
            //第二參數為true時,刪除目錄,包括子目錄和文件
            try
            {
                System.IO.Directory.Delete(@"C:/testDir/subTestDir", true);
            }
            catch(System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }

(2)通過FileInfo對象方法刪除目錄
復制代碼 代碼如下:

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:/testDir/subTestDirTwo");
            try
            {
                //無參數刪除空目錄
                //當參數為false,可刪除空目錄;為true,刪除目錄,包括子目錄和文件
                di.Delete(true);
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }

2、刪除文件
刪除文件時如果指定文件的目錄存在,而文件不存在,則不會拋出異常,如果指定文件的目錄不存在,則會拋出異常。
(1)通過File類Delete方法刪除文件
復制代碼 代碼如下:

try
                {
                    System.IO.File.Delete(@"C:/testDir/subTestDir/test.txt");
                }
                catch(System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                }

(2)通過FileInfo對象Delete方法刪除文件
復制代碼 代碼如下:

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/testDir/subTestDir/test1.txt");
            try
            {
                fi.Delete();
            }
            catch(System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定西市| 乳源| 怀安县| 伊宁县| 黄浦区| 吉林省| 阳山县| 长泰县| 扎鲁特旗| 社会| 平阴县| 闸北区| 香河县| 金阳县| 巧家县| 海口市| 光山县| 晋城| 甘南县| 垦利县| 吉林市| 宜都市| 珲春市| 武清区| 田林县| 阿城市| 万山特区| 通海县| 磐石市| 永安市| 微山县| 临朐县| 邯郸市| 罗甸县| 万州区| 德化县| 林芝县| 沿河| 忻州市| 邮箱| 鄯善县|