【问题标题】:Program not able to write to a newly made text file程序无法写入新制作的文本文件
【发布时间】:2013-03-07 10:58:06
【问题描述】:

在其他用户在这个问题上提出了一些很棒的论点: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

关于如何写入相关文本文件的任何想法?

【问题讨论】:

    标签: vb.net text-files streamwriter


    【解决方案1】:

    很有可能您的应用程序的前一次迭代未能正确关闭对 StreamWriter 中文件的访问。由于您的构造函数设置为覆盖(而不是附加)到文件,这可能是源。

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

    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
    

    使用 Using 块和 close 语句看起来确实是多余的,但这可以确保即使发生异常也可以访问您的文件。

    【讨论】:

    • 谢谢 :) 以这种方式完美运行。
    【解决方案2】:

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

    然后,为避免文件流未正确关闭的问题,请尝试使用Using statement,以确保正确关闭和处置您的文件资源(无需在 Using 中调用 close)。

    另外,请注意,如果文件不存在或者您希望覆盖以前的内容(为 Append 标志传递 false),StreamWriter 完全能够创建文件。
    所以你的代码可以减少到这些行。

    ' 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
    

    【讨论】:

    • 谢谢,这行得通,虽然我更喜欢将文件保存在我在问题中使用的目录中。
    • 枚举 SpecialFolder.CommonApplicationData 在 Win7 中解析为 C:\PROGRAMDATA,在 XP 中解析为 C:\DOCUMENTS & SETTINGS\ALLUSER...。因此,这是独立于底层操作系统的方法。
    • 哦,我明白了,我认为SpecialFolder.CommmonApplicationData 是 %appdata% 区域中某个位置的引用。我的坏
    • 顺便说一句,您对directories used in my question 的含义还不清楚。我认为将数据写入 Windows 工程师专门为此考虑的文件夹中会更好(更专业?)
    • 嗯,我个人更喜欢在我的 C:\ 驱动器上保存尽可能少的文件,这是用 SpecialFolder.CommonApplicationData 存储文件的位置(W7 的标准 = C:\ProgramData) ,所以我正在考虑将文件保存到保存实际程序的驱动器上(在我的情况下,它在我的 D:\ 驱动器上),使用我问题中的代码,文件最终将保存在 D:\ProgramData \...我想这主要是个人喜好,而不是我的聪明:)
    【解决方案3】:

    File.Create 返回一个打开的FileStream,您应该在后续传递给StreamWriter 构造函数,而不是再次传递文件名,或者您可以完全省略File.Create 调用。

    您可能想查看StreamWriterUsing 块,以便它可以预见地关闭。

    【讨论】:

    • 所以我应该可以使用粘贴在这个 pastebin 中的代码? link,省略检查文件是否存在、创建目录和创建文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多