程序无法写入新建的文本文件

问题描述:

在此问题中其他用户提出了一些重要论点:How to write to a text file inside of the application后,我决定不使用资源文件,而是在文件夹中创建一个文件&然后从/那里。程序无法写入新建的文本文件

虽然由于某种原因,我似乎无法写入有问题的文件,但它一直抛出异常,告诉我该文件已被另一进程使用。

下面是我用于写入此文件的代码。

If System.IO.File.Exists(credentials) Then 
         Dim objWriter As New System.IO.StreamWriter(credentials, False) 
         objWriter.WriteLine(remember) 
         objWriter.Close() 
        Else 
         System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel") 
         System.IO.File.Create(credentials) 
         Dim objWriter As New System.IO.StreamWriter(credentials, False) 
         objWriter.WriteLine(remember) 
         objWriter.Close() 
        End If 

关于如何写入有问题的文本文件的任何想法?

您正试图在公共应用程序数据目录中创建一个目录。应该使用Environment类方法和枚举找到该目录,因为在操作系统之间不同。但是,您使用值为credentials作为文件名。我想你想要将数据文件存储在公共应用程序目录中,而不是在没有写入数据文件权限的地方(如C:\ program files(x86))。

然后,以避免问题的文件流无法正常关闭,尝试使用,保证您的文件资源的正确关闭和处置Using statement(无需千钧一发内使用)。

此外,请注意,StreamWriter完全有能力创建该文件,如果它不存在或如果您希望覆盖以前的内容(为Append标志传递false)。
所以你的代码可以简化为这些行。

' Get the common application data directory (could be different from Win7 and XP) 
Dim workDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) 
' Combine with your own working data directory 
workDir = Path.Combine(workdir, "DayZAdminPanel") 
' Create if not exists 
If Not Directory.Exists(workDir) Then 
    Directory.CreateDirectory(workDir) 
End If 

' Create/Overwrite your data file in a subfolder of the common application data folder 
Dim saveFile = Path.Combine(workDir, Path.GetFileName(credentials)) 
Using objWriter = New System.IO.StreamWriter(saveFile, False) 
    objWriter.WriteLine(remember) 
End Using 
+0

谢谢,这是有效的,但我更喜欢将文件保存在我的问题中使用的目录中。 – Yorrick 2013-03-18 22:42:00

+0

Enum'SpecialFolder.CommonApplicationData'在Win7中解析为C:\ PROGRAMDATA,在C:\ DOCUMENTS&SETTINGS \ ALLUSER ...中为XP。所以,这是独立于底层操作系统的方式。 – Steve 2013-03-18 22:44:40

+0

哦,我看到了,我认为'SpecialFolder.CommmonApplicationData'是%appdata%区域中位置的参考。我的坏 – Yorrick 2013-03-18 23:08:52

File.Create返回打开FileStream,你应该传递到StreamWriter构造上的后续,而不是再次通过文件名,或者你可以省略File.Create呼叫干脆。

您可能想要查看Using区块的StreamWriter,以便它可预测地关闭。

+0

所以我应该能够使用粘贴在这个pastebin中的代码? [链接](http://pastebin.com/X62QxP7U),省略检查文件是否存在,目录创建和文件创建? – Yorrick 2013-03-18 22:38:52

以前的应用程序迭代很有可能无法正确关闭对StreamWriter中文件的访问。由于你的构造函数被设置为覆盖(而不是追加)到文件中,所以这可能是源代码。

尝试用“使用”设置您的应用程序语句正常打开/关闭文件:

If System.IO.File.Exists(credentials) Then 

    Using objWriter As StreamWriter = New StreamWriter(credentials, False) 
     objWriter.WriteLine(remember) 
     objWriter.Close() 
    End Using 

Else    

    System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel") 
    System.IO.File.Create(credentials) 

    Using objWriter As StreamWriter = New StreamWriter(credentials, False) 
     objWriter.WriteLine(remember) 
     objWriter.Close() 
    End Using 

End If 

它看起来确实冗余有一个使用块和密切的声明,但是这确保访问即使发生异常也是如此。

+0

谢谢:)以这种方式完美工作。 – Yorrick 2013-03-18 22:42:22