gpt4 book ai didi

c# - 试图创建一个文件并立即删除它

转载 作者:太空狗 更新时间:2023-10-29 18:03:40 25 4
gpt4 key购买 nike

// (1) create test file and delete it again
File.Create(Path.Combine(folder, "testfile.empty"));
File.Delete(Path.Combine(folder, "testfile.empty"));

最后一行抛出异常:

The process cannot access the file '\\MYPC\C$_AS\RSC\testfile.empty' because it is being used by another process.

这是为什么?

最佳答案

File.Create 返回一个您尚未关闭的流。

using(var file = File.Create(path)) {
// do something with it
}
File.Delete(path);

应该工作;或更简单:

File.WriteAllBytes(path, new byte[0]);
File.Delete(path);

甚至只是:

using(File.Create(path)) {}
File.Delete(path);

关于c# - 试图创建一个文件并立即删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5313231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com