2014-11-05 31 views
0

好的,所以我的程序檢查看到的是一個文本文件存在(這工作),那麼如果它不存在它創建目錄和文件,以及將數據保存到文件。當它落入第二個測試語句以創建文本文件時,我收到錯誤。 該程序將創建目錄和文件,但無法寫入它。 「unhandeled exception:文件正在被另一個進程使用,如果我點擊忽略錯誤並運行程序,我可以再次單擊保存按鈕並且它可以正常工作延遲創建文件時使用代碼?

所以我想我的問題是,我可以以某種方式延緩寫入到文件足夠長的其他操作完成,或是否有更好的方法來組織它的代碼,所以它不是一個問題

相關代碼:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 

    'create necessary directory 
    'create text file 
    'if file exist write to file 

    If My.Computer.FileSystem.FileExists("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") = True Then 
     MsgBox("Data Saved") 

     Using sr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") 
      For Each line In lstColors.Items 
       sr.WriteLine(line) 
      Next 
      sr.Close() 
     End Using 

    ElseIf My.Computer.FileSystem.FileExists("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") = False Then 

     My.Computer.FileSystem.CreateDirectory("C:\Documents and Settings\All Users\Documents\NailPolishSelector") 
     Dim fs As FileStream = File.Create("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") 

      'error occurs here 
     Using srr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") 
      For Each line In lstColors.Items 
       srr.WriteLine(line) 
      Next 
      srr.Close() 
     End Using 

     MsgBox("File Created") 
    End If 


End Sub 
+0

你能解釋downvote嗎?我不明白我的問題有什麼問題。我搜索並沒有發現類似的東西。 – Aaron 2014-11-05 20:48:15

+0

我本來會問同樣的問題。 – logixologist 2014-11-05 20:49:11

+2

取出'File.Create'。 StreamWriter將創建文件; 'File.Create'給你一個它創建的文件的句柄,但你不需要它。另外,使用'If = True'不是必須的,我建議你刪除'= True'部分以使其更具可讀性,並且使用'Not'而不是'= False'。 – 2014-11-05 20:49:42

回答

3

你的問題是該文件被fs FileStream對象鎖定,因此srr StreamWriter無法寫入它:這就像試圖通過它的名字

去的文件的引用,以便與

Using srr As New IO.StreamWriter(fs) 

更換

Using srr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") 

應該解決您的問題。

+0

我想我會和安德魯所做的評論一起去,但是因爲這也可行,而且這是我接受它的唯一答案,所以任何未來的人都知道它已經解決了。 – Aaron 2014-11-05 21:05:28

1

您的代碼可以重新編寫,使其更簡單,更健壯。我猜測,如果存在該文件要替換它,但如果沒有,那麼請刪除'OPTIONAL一段代碼:(感謝去馬格努斯您指出File.AppendAllLines(path, lstColors.Items)

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 

    ' Get the All Users documents folder 
    Dim docsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) 
    ' Combine it with the desired directory and filename 
    Dim theFile As String = Path.Combine(docsFolder, "NailPolishSelector\polishColors.txt") 

    'OPTIONAL 
    ' Remove the previous version of the file 
    If My.Computer.FileSystem.FileExists(theFile) Then 
     File.Delete(theFile) 
    End If 

    ' REQUIRED: Create the directory if it doesn't exist 
    If Not Directory.Exists(Path.GetDirectoryName(theFile)) Then 
     Directory.CreateDirectory(Path.GetDirectoryName(theFile)) 
    End If 

    ' Create the data file 
    File.AppendAllLines(theFile, lstColors.items) 

    MsgBox("Data saved.") 

End Sub 

有幾件事可以在文檔中查找:使用SpecialFolders enumerationPath.Combine

+0

肯定會看看推薦的文檔,所以我明白它在向前推進。謝謝你的幫助,非常感激。我想我會保持現在的狀態,並且在備份中改變它來練習。 – Aaron 2014-11-05 21:34:23