1. 程式人生 > >根據檔案獲取資料夾,判斷資料夾是否存在,不存在則建立

根據檔案獲取資料夾,判斷資料夾是否存在,不存在則建立

開發環境:VS2008

語言:C#

功能:根據檔案獲取檔案所在資料夾,並判斷資料夾是否存在子資料夾2

如果不存在子資料夾2,則建立

string fileName = "D:\\2\\2.txt";
            string myDir = System.IO.Path.GetDirectoryName(fileName);
            //執行結果
            //myDir為 D:\\2

            string tempDir = myDir + "\\2\\";
            //判斷資料夾是否存在
            if (!System.IO.Directory.Exists(tempDir))
            {
                //資料夾不存在則建立該資料夾
                System.IO.Directory.CreateDirectory(tempDir);
            }