我想在用戶文件夾中創建一個文件並用一些基本文本填充它。看似簡單,但我不斷收到一個錯誤:在c中創建一個文件和一個文件夾#
Could not find a part of the path 'C:\websites\admin\upload\users\testuserID\testuserIDSampleRecord.txt'."} System.Exception {System.IO.DirectoryNotFoundException}
我的網站位於:c:\websites\testsite\
,所以完整的路徑應該是:
c:/websites/testsite/admin/upload/users/
的IIS本地主機設置爲指向c:/websites/
所以當我運行它,我鍵入localhost/testsite
得到它
這裏是我有:
try
{
string SampleCaseText = BuildTextRecord();
string username = (string)Session["userid"];
string folderPath = "/testsite/admin/upload/users/" + username;
bool IsExists = System.IO.Directory.Exists(Server.MapPath(folderPath));
if(!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(folderPath));
System.IO.File.Create(folderPath + "/" + username + "SampleRecord.txt");
File.WriteAllText(Path.Combine(folderPath, username + "SampleRecord.txt"), SampleCaseText);
}
它在正確的位置創建了新文件夾testuserID,但嘗試創建/寫入文件時失敗。
嘗試使用'System.Path.CombinePath'函數,而不是字符串連接。另外,我認爲對'System.IO.File.Create'的調用應該使用Windows而不是web'/' – freefaller 2013-04-05 15:46:48
你得到的錯誤是什麼? – Arshad 2013-04-05 15:48:09
訪問權限怎麼樣?你可以檢查是否創建文件夾通過Windows資源管理器進入目錄? – Yahya 2013-04-05 15:48:16