2013-01-14 118 views
1

所以,我試圖創建一個.txt文件,然後寫一些愚蠢的數據。但是我收到了分享違規。我覺得這可能是因爲我試圖在創建文件後直接爲文件創建StreamWriter,但這沒有意義。所以我有點失落。我嘗試刪除課程中除錯誤行之外的所有其他StreamWriters和讀者,並且我仍然遇到違規行爲。我得到的錯誤,創建文件後發生文件共享衝突?

IOException: Sharing violation on path C:\Users\USER\Desktop\Accessible\Assets\IO\Books\A community of learners\frog\frog_status.txt 
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) 
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) 
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) 

點到線:

StreamWriter sw = new StreamWriter(statusTxtPath); 

以下功能:

private IEnumerator GatherStatusInfo(string folderPath, string mediaName) { 

     string statusTxtPath = folderPath + @"/" + mediaName + "_status.txt"; 
     _currentInfoBeingCreated._readingStatusTxtPath = statusTxtPath; 

     if(BuildManager.instance._isResetStatusFilesWhenStarting) { 
      if(File.Exists(statusTxtPath)) 
       File.Delete(statusTxtPath); 
     } 

     if(!File.Exists(statusTxtPath)) { 
      File.Create(statusTxtPath); 
      StreamWriter sw = new StreamWriter(statusTxtPath); 
      sw.WriteLine(MediaStatus.Unread); 
      sw.WriteLine("0"); 
      _currentInfoBeingCreated._status = MediaStatus.Unread; 
      _currentInfoBeingCreated._pageLastRead = 0; 
      sw.Flush(); 
      sw.Close(); 

     } else { 

      StreamReader statusReader = new StreamReader(statusTxtPath); 
      _currentInfoBeingCreated._status = (MediaStatus) Enum.Parse(typeof(MediaStatus), statusReader.ReadLine()); 
      _currentInfoBeingCreated._pageLastRead = (int) ConversionUtils.instance.String2Float(statusReader.ReadLine()); 
      statusReader.Close(); 
     } 

     yield return 0; 
    } 

任何想法,爲什麼狗不會打獵?

+2

可能是File.Create將它保持打開狀態?我一直讓StreamWriter創建文件。 –

回答

3

是否有一個原因,你想創建該文件,然後重新打開它來寫入它。 StreamWriter有一個方法可以做到這一點。如果它不存在,它將創建一個新文件。

從以上鍊接:

初始化指定路徑上的指定文件StreamWriter類的新實例,使用默認的編碼和緩衝區大小。如果文件存在,它可以被覆蓋或附加到。如果該文件不存在,則此構造函數將創建一個新文件。

+0

太棒了。我會試試看。乾杯。 – Catlard

+0

工作過。謝謝馬克。 – Catlard

+0

@Catlard歡迎您,很高興幫助 –

1

可能會發生這種情況,因爲StreamWriter尚未釋放未管理的資源: 嘗試使用using的塊,而不是手動打開和關閉它。

using (StreamWriter writer = new StreamWriter(statusTxtPath)) 
    { 
     // do work here 
    } 
+1

也有效。謝謝! – Catlard

3

StreamWriter無法訪問該文件,因爲File.Create返回FileStream你沒有消耗。

如上所述,File.Create是沒有必要的。您還可以使用:

using (var writer = new StreamWriter(File.Create(statusTxtPath))) 
{ 
    // do work here. 
} 

這將消耗文件流並關閉它。無論何時使用流和大多數與流進行交互的類,請務必使用using()塊來確保正確釋放句柄。

1

我是這種編程語言的新手,我知道一些Ansi C主要是編程微控制器,但我現在正在學習C#,而且我遇到了同樣的問題。

System.IO.IOException
共享衝突的路徑/ .../...

我發現,具有下面的代碼,將引發異常。

if(!File.Exists(path)) 
    File.Create(path); 

我發現解決這個問題的解決方案實際上使用的代碼更少,更簡單。

File.AppendAllText(path, str); 

這一行程序會打開該文件,如果它已經存在和文本追加到它的結束,如果不存在,它會創建並追加的文本。

是: 路徑 - >文件的路徑
                      海峽        - >要寫入文件

文本Xamarin論壇頁面上找到this解決方案的提示。

-1

我也面臨着與多線程編程相同的問題,我想它的線程同步問題,但問題是在File.Create()之後我用StreamWriter。在這裏,您可以嘗試使用兩個不同的步驟,您可以在一個步驟中進行組合。這段代碼對我來說工作得很好。

using (var writer = new StreamWriter(File.Exists(path) ? File.Open(path, FileMode.Open) : File.Create(path))) 
{ 
    writer.Write(str); 
    writer.Close(); 
} 
+0

這似乎不回答共享違規問題。 –