且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将文本附加到文本文件

更新时间:2023-01-03 08:14:24

前几天,我在将应用程序移植到.NET时遇到了这种情况.

与用于附加模式的Win32 fopen()不同,它是原子的,文件是
是在共享读/写模式下打开的,该文件在.NET下针对
打开的文件 追加模式,在共享只读"模式下打开.

您必须同步带有锁的附加文件或打开
首先以共享READ/WRITE模式共享文件,然后将此文件传递给流至
附加.请参见File.Open(string,FileMode,FileAccess,FileShare)
方法,我不记得我的VB.NET,但它是这样的:

私有子SaveData(以字符串形式获取byByval strStuInfo)
Dim fs as FileStream
fs = File.Open(_path,FileMode.Append,_
FileAccess.Write,_
FileShare.ReadWrite)
fs.WriteLine(EncryptText(strStuInfo,_password)
fs.Close()
结束子

同样,问题是

File.AppendText(fileName)

方法,因为它以FileShare.Read Only模式打开文件,因此
阻止任何其他线程和/或进程附加或读取文件.
它与C/C ++ fopen()运行时库标准中的
不同 I/O函数在其中打开要追加的内容,即fopen(fileName,"at")打开
以文件共享读/写兼容模式进行文件操作,允许
不同的线程来追加到文件.

就是说,我的微软一定要做出这个内在的原因
行为为FileShare.Read.它是通过裂缝掉落的,还是
在.NET下不建议使用上述内容是有原因的.

也许有WIN32和.NET内部文件I/O经验的人
可以告诉我们.在上述
之前,我不打算发布任何代码 确定.


kimhout写道:

>你好
>
>有人可以帮我吗?
>
>私有Sub btnOk_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btnOk.Click
> Me.SaveData("testing")
>结束子
>
>私有子SaveData(ByVal strStuInfo作为字符串)
> Me.chkExistFile()
> '代码在创建文件后始终在下面附加错误
> _strWrite = File.AppendText(_path)
> _strWrite.WriteLine(EncryptText(strStuInfo,_password))
> _strWrite.Flush()
> _strWrite.Close()
>结束子
>
>私人子chkExistFile()
>如果File.Exists(_path)然后
>其他
> File.Create(_path)
>如果结束
>结束子
>
>当文件不存在时,它将创建一个文本,然后在该文本后附加一个文本->然后
>总是错误(该进程无法访问文件
> 'C:\ Users \ Administrator \ Desktop \ W-LTS \ bin \ Debug \ Student.txt',因为它正在
>被另一个进程使用.)那么我该怎么办?
>
>但如果存在,我可以追加文字,则可以正常工作.
>
>请帮助
>
>谢谢
>




Hello

can anyone help me?

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    Me.SaveData("testing")
  End Sub

  Private Sub SaveData(ByVal strStuInfo As String)
    Me.chkExistFile()
'the code append below alway error after create file
    _strWrite = File.AppendText(_path)
    _strWrite.WriteLine(EncryptText(strStuInfo, _password))
    _strWrite.Flush()
    _strWrite.Close()
  End Sub

  Private Sub chkExistFile()
    If File.Exists(_path) Then
    Else
      File.Create(_path)
    End If
  End Sub

when the file is not exists it will create than append a text to it -> then it always error( The process cannot access the file 'C:\Users\Administrator\Desktop\W-LTS\bin\Debug\Student.txt' because it is being used by another process.) so how can i do?

but if the exists i can append text, it works properly.

plz help

thank

I ran into this the other day when porting my application to .NET.

Unlike Win32 fopen() for append mode, where it is atomic and the file
is open in a share Read/Write mode, under .NET the file opened for
append mode, is opened in a SHARE READ ONLY mode.

You either haveto synchronize the file appending with a lock or open a
file first in a share READ/WRITE mode and pass this to a stream to
append. See the File.Open(string, FileMode, FileAccess, FileShare)
method, I don't remember my VB.NET, but it would be like this:

Private Sub SaveData(Byval strStuInfo As String)
Dim fs as FileStream
fs = File.Open(_path, FileMode.Append, _
FileAccess.Write, _
FileShare.ReadWrite)
fs.WriteLine(EncryptText(strStuInfo, _password)
fs.Close()
end Sub

Again, the problem is the

File.AppendText(fileName)

method because it opens the file in FileShare.Read only mode, thus
preventing any other thread and/or process to append or read the file.
Its not the same as in the C/C++ fopen() runtime library standard
I/O function where opening for append, i.e. fopen(fileName,"at") opens
the file in a file share read/write compatible mode allowing for
different threads to append to a file.

That said, there has to be a reason my Microsoft made this intrinsic
behavior to be FileShare.Read only. Either it fell thru the cracks, or
there is a reason why the above is not recommended under .NET.

Maybe someone with both WIN32 and .NET internal file I/O experience
can tell us. I don't plan to release any code until the above is
determined.


kimhout wrote:

> Hello
>
> can anyone help me?
>
> Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
> Me.SaveData("testing")
> End Sub
>
> Private Sub SaveData(ByVal strStuInfo As String)
> Me.chkExistFile()
> 'the code append below alway error after create file
> _strWrite = File.AppendText(_path)
> _strWrite.WriteLine(EncryptText(strStuInfo, _password))
> _strWrite.Flush()
> _strWrite.Close()
> End Sub
>
> Private Sub chkExistFile()
> If File.Exists(_path) Then
> Else
> File.Create(_path)
> End If
> End Sub
>
> when the file is not exists it will create than append a text to it -> then it
> always error( The process cannot access the file
> 'C:\Users\Administrator\Desktop\W-LTS\bin\Debug\Student.txt' because it is being
> used by another process.) so how can i do?
>
> but if the exists i can append text, it works properly.
>
> plz help
>
> thank
>