正在被另一個進程使用的文件我試圖檢測一個文件是否在運行時存在,如果沒有,請創建它。但是,當我嘗試寫入該錯誤時,出現以下錯誤:File.Create()
進程無法訪問文件'myfile.ext',因爲它正在被另一個進程使用。
string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre);
if (!File.Exists(filePath))
{
File.Create(filePath);
}
using (StreamWriter sw = File.AppendText(filePath))
{
//write my text
}
關於如何解決它的任何想法?
我似乎沒有收尾選項。這裏是代碼: string filePath = string.Format(@「{0} \ M {1} .dat」, ConfigurationManager.AppSettings [「DirectoryPath」], costCentre);如果(!File.Exists(filePath)) { File.Create(filePath); } 使用(StreamWriter的SW = File.AppendText(文件路徑)){ // 寫我的文字 } – Brett 2010-05-06 13:23:27
'File.Create'返回'FileStream'和具有'關閉()' – 2015-09-07 20:49:04