2014-11-05 32 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