.NET File.Create,之后无法删除文件

时间:2022-11-07 07:32:43

Using method: System.IO.File.Create()

使用方法:System.IO.File.Create()

After the file gets created, it still remains used by a process, and I can't delete it.

文件创建后,它仍然由进程使用,我无法删除它。

Any idea how I can better create the file, should be a 0byte file, and then somehow close and dispose?

任何想法我怎么能更好地创建文件,应该是一个0byte文件,然后以某种方式关闭和处置?

6 个解决方案

#1


26  

JL,

You should wrap your call to .Create in a using statement so that the FileStream that .Create returns will be closed properly. IE:

您应该在using语句中包含对.Create的调用,以便正确关闭.Create返回的FileStream。 IE:

using (File.Create("path")){...}

#2


14  

The Create method not only creates the file, it opens it and return a FileStream object that you can use to write to the file.

Create方法不仅创建文件,还会打开它并返回可用于写入文件的FileStream对象。

You have to close the file after yourself, otherwise it will not be closed before the garbage collector cleans up the FileStream object.

您必须自己关闭文件,否则在垃圾收集器清理FileStream对象之前不会关闭它。

The easiest way is to simply close the file using the reference that the Create method returns:

最简单的方法是使用Create方法返回的引用简单地关闭文件:

File.Create(fileName).Close();

#3


12  

nikmd23 has the short answer, the long answer is: the FileStream that File.Create(...) is returning is not being deterministically disposed of, therefore it's file handle is not closed when you're trying to delete it.

nikmd23有简短的答案,答案很长:File.Create(...)返回的FileStream没有被确定性地处理掉,因此当你试图删除它时,它的文件句柄没有被关闭。

As nikmd23 put it, wrapping your File.Create(...) call will with a using statement will make sure the stream is closed and disposed of:

正如nikmd23所说的那样,使用using语句包装File.Create(...)调用将确保关闭并处理流:

using (FileStream fs = File.Create(path)) { 
  // do anything with the stream if need-be...
}
File.Delete(path); //after it's been disposed of.

The using(...) block is really just compiler-sugar for:

using(...)块实际上只是编译器 - 糖:

FileStream fs = File.Create(path);
try { 
   // do anything with the stream if need-be...
}
finally { 
  fs.Dispose();
}
File.Delete(path)

#4


1  

You should use nikmd23's answer in almost all cases. If you can't, because you need to pass the FileStream somewhere else, make sure to call the FileStream.Close method eventually. Preferably you would have the class that 'owns' the FileStream implement IDisposable itself, and close the stream in its Dispose method.

几乎在所有情况下都应该使用nikmd23的答案。如果你不能,因为你需要在其他地方传递FileStream,请确保最终调用FileStream.Close方法。您最好拥有“拥有”FileStream实现IDisposable本身的类,并在其Dispose方法中关闭该流。

For more information on implementing IDisposable, refer to the MSDN documentation. Easier reading, and more up to date, is Joe Duffy's post on the subject.

有关实现IDisposable的更多信息,请参阅MSDN文档。更容易阅读和更新的是Joe Duffy关于这个主题的帖子。

#5


1  

using(FileStream f = File.Create(file_path))
{
  // ... do something with file
  f.Close();
}

The "f.Close();" line closing file immediately. If not close manually, Disposing may not close it.

“f.Close();”行立即关闭文件。如果不手动关闭,处理可能不会关闭它。

#6


0  

See System.IO.File.Create(String) Method paramter and return value description

请参见System.IO.File.Create(String)方法参数和返回值描述

Parameters

path Type: System.String The path and name of the file to create.

path类型:System.String要创建的文件的路径和名称。

Return Value

Type: System.IO.FileStream

A FileStream that provides read/write access to the file specified in path.

FileStream,提供对path中指定的文件的读/写访问权限。

The FileStream return value is there for IO access to the created file. If you are not interested in writing (or reading) the newly created file, close the stream. That is what the using block is ensuring.

FileStream返回值用于IO访问创建的文件。如果您对编写(或读取)新创建的文件不感兴趣,请关闭该流。这就是使用块确保的。

#1


26  

JL,

You should wrap your call to .Create in a using statement so that the FileStream that .Create returns will be closed properly. IE:

您应该在using语句中包含对.Create的调用,以便正确关闭.Create返回的FileStream。 IE:

using (File.Create("path")){...}

#2


14  

The Create method not only creates the file, it opens it and return a FileStream object that you can use to write to the file.

Create方法不仅创建文件,还会打开它并返回可用于写入文件的FileStream对象。

You have to close the file after yourself, otherwise it will not be closed before the garbage collector cleans up the FileStream object.

您必须自己关闭文件,否则在垃圾收集器清理FileStream对象之前不会关闭它。

The easiest way is to simply close the file using the reference that the Create method returns:

最简单的方法是使用Create方法返回的引用简单地关闭文件:

File.Create(fileName).Close();

#3


12  

nikmd23 has the short answer, the long answer is: the FileStream that File.Create(...) is returning is not being deterministically disposed of, therefore it's file handle is not closed when you're trying to delete it.

nikmd23有简短的答案,答案很长:File.Create(...)返回的FileStream没有被确定性地处理掉,因此当你试图删除它时,它的文件句柄没有被关闭。

As nikmd23 put it, wrapping your File.Create(...) call will with a using statement will make sure the stream is closed and disposed of:

正如nikmd23所说的那样,使用using语句包装File.Create(...)调用将确保关闭并处理流:

using (FileStream fs = File.Create(path)) { 
  // do anything with the stream if need-be...
}
File.Delete(path); //after it's been disposed of.

The using(...) block is really just compiler-sugar for:

using(...)块实际上只是编译器 - 糖:

FileStream fs = File.Create(path);
try { 
   // do anything with the stream if need-be...
}
finally { 
  fs.Dispose();
}
File.Delete(path)

#4


1  

You should use nikmd23's answer in almost all cases. If you can't, because you need to pass the FileStream somewhere else, make sure to call the FileStream.Close method eventually. Preferably you would have the class that 'owns' the FileStream implement IDisposable itself, and close the stream in its Dispose method.

几乎在所有情况下都应该使用nikmd23的答案。如果你不能,因为你需要在其他地方传递FileStream,请确保最终调用FileStream.Close方法。您最好拥有“拥有”FileStream实现IDisposable本身的类,并在其Dispose方法中关闭该流。

For more information on implementing IDisposable, refer to the MSDN documentation. Easier reading, and more up to date, is Joe Duffy's post on the subject.

有关实现IDisposable的更多信息,请参阅MSDN文档。更容易阅读和更新的是Joe Duffy关于这个主题的帖子。

#5


1  

using(FileStream f = File.Create(file_path))
{
  // ... do something with file
  f.Close();
}

The "f.Close();" line closing file immediately. If not close manually, Disposing may not close it.

“f.Close();”行立即关闭文件。如果不手动关闭,处理可能不会关闭它。

#6


0  

See System.IO.File.Create(String) Method paramter and return value description

请参见System.IO.File.Create(String)方法参数和返回值描述

Parameters

path Type: System.String The path and name of the file to create.

path类型:System.String要创建的文件的路径和名称。

Return Value

Type: System.IO.FileStream

A FileStream that provides read/write access to the file specified in path.

FileStream,提供对path中指定的文件的读/写访问权限。

The FileStream return value is there for IO access to the created file. If you are not interested in writing (or reading) the newly created file, close the stream. That is what the using block is ensuring.

FileStream返回值用于IO访问创建的文件。如果您对编写(或读取)新创建的文件不感兴趣,请关闭该流。这就是使用块确保的。