vb.net File.Create() 之后的“文件正在被另一个进程使用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19705468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"File is being used by another process" after File.Create()
提问by Brandon
I have an application that creates a text file if the file doesn't already exist and then writes something to it. It works great the first time through, when I am creating and immediately writing to the file. My problem is that the next time this code executes and the file is already created, it throws an exception when I am trying to write to the file. I get the "File is being used by another process" error.
我有一个应用程序,如果文件不存在,它会创建一个文本文件,然后向其中写入一些内容。当我创建并立即写入文件时,它在第一次运行时效果很好。我的问题是,下次执行此代码并且文件已创建时,当我尝试写入文件时会引发异常。我收到“另一个进程正在使用文件”错误。
So it would seem I need to close the file after I create it? I don't see how to do this though, but it is probably something very simple. I'll post some code but its not really necessary, I'm just using a vanilla flavored string builder and stream writer.
所以看起来我需要在创建文件后关闭它?我不知道如何做到这一点,但这可能非常简单。我会发布一些代码,但它并不是真正必要的,我只是使用香草风味的字符串构建器和流编写器。
Private Sub createFileLocations()
If Not Directory.Exists("./Path") Then
Directory.CreateDirectory("./Path")
End If
If clsGeneralSettings.Printer1 IsNot Nothing Then
If Not File.Exists("./Path/File1" & ".txt") Then
File.Create("./Path/File1" & ".txt")
End If
End If
End Sub
Private Sub AppendTextFile(randomId As String, PrintDate As Date, PrintName As String)
Try
Dim _stringBuilder As StringBuilder = New StringBuilder
Dim _StreamWriter As StreamWriter
Dim fileName As String
If PrintName = clsGeneralSettings.Printer1 Then
fileName = "./Path/File1" & ".txt"
qPrinter1.Enqueue(randomId)
If qPrinter1.Count > 10 Then
qPrinter1.Dequeue()
End If
_stringBuilder.AppendLine(PrintDate + " | " + randomId)
_StreamWriter = New StreamWriter(fileName, True)
End If
'Todo: Figure this out
Using _StreamWriter
_StreamWriter.Write(_stringBuilder.ToString)
_StreamWriter.Flush()
_StreamWriter.Close()
_stringBuilder.Clear()
End Using
Catch ex As Exception
End Try
End Sub
回答by Jehof
The problematic code/line is this
有问题的代码/行是这个
If Not File.Exists("./PalletQueue/Printer1" & ".txt") Then
File.Create("./PalletQueue/Printer1" & ".txt")
End If
File.Createreturns a FileStream, that you need to close, if you want to write later to that file. Changing your code to the following should solve your problem.
File.Create返回一个 FileStream,如果您想稍后写入该文件,您需要关闭它。将您的代码更改为以下内容应该可以解决您的问题。
If Not File.Exists("./PalletQueue/Printer1" & ".txt") Then
Dim file as FileStream = File.Create("./PalletQueue/Printer1" & ".txt")
file.Close()
End If
回答by dbasnett
You could eliminate the file create logic you have and let the StreamWriter create the file.
您可以消除您拥有的文件创建逻辑,让 StreamWriter 创建文件。
http://msdn.microsoft.com/en-us/library/36b035cb%28v=vs.110%29.aspx
http://msdn.microsoft.com/en-us/library/36b035cb%28v=vs.110%29.aspx
回答by Sir Crispalot
Have a look at the documentation:
看看文档:
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.
此方法创建的 FileStream 对象的默认 FileShare 值为 None;在原始文件句柄关闭之前,没有其他进程或代码可以访问创建的文件。
You have an open handle to the FileStream after running the File.Create()method.
运行该File.Create()方法后,您拥有 FileStream 的打开句柄。
回答by Badharudeen
You have missed to dispose some image it may be current image or original image
you can do this like CurrImage.Dispose() & OriginalImage.Dispose()
您错过了处理某些图像,它可能是当前图像或原始图像,您可以这样做 CurrImage.Dispose() & OriginalImage.Dispose()

