C# 覆盖/保存当前文件

发布于 2024-12-08 02:25:01 字数 304 浏览 0 评论 0原文

我正在用 C#、Windows 窗体做编辑器。我希望将文件的“新内容”保存在同一个文件中(通常使用“保存”选项),但我收到 IOException,[该进程无法访问文件“文件名”,因为它正在被另一个进程使用。 我有写入新文件的方法并且它有效。如何使用它来覆盖当前文件。

编辑: 我正在使用二进制编写器 http://msdn.microsoft.com/en-us/library /atxb4f07.aspx

I am doing editor in c#, windows forms. I wish to save 'new content' of file in the same file (usual usage of 'save' option) but I receive IOException, [ the process cannot access the file ' filename' because it is being used by another process.
I have method that writes to a NEW file and it works. How to use it to overwrite current file.

Edit:
I am using binarywriter http://msdn.microsoft.com/en-us/library/atxb4f07.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单爱 2024-12-15 02:25:01

很可能当您加载文件时,您没有关闭 FileStream 或用于读取该文件的任何内容。始终对您的信息流使用using 语句 (以及其他实现 IDisposable 的类型),这应该不是问题。 (当然,如果您实际上在单独的应用程序中打开该文件,那完全是一个不同的问题。)

因此,

// Bad code
StreamReader reader = File.OpenText("foo.txt");
string data = reader.ReadToEnd();
// Nothing is closing the reader here! It'll keep an open
// file handle until it happens to be finalized

您应该使用更像这样的内容:

string data;
using (TextReader reader = File.OpenText("foo.txt"))
{
    data = reader.ReadToEnd();
}
// Use data here - the handle will have been closed for you

或者理想情况下,使用 File 为您完成这一切:

string text = File.ReadAllText("foo.txt");

Chances are that when you loaded the file, you didn't close the FileStream or whatever you used to read it. Always use a using statement for your streams (and other types implementing IDisposable), and it shouldn't be a problem. (Of course if you actually have that file open in a separate application, that's a different problem entirely.)

So instead of:

// Bad code
StreamReader reader = File.OpenText("foo.txt");
string data = reader.ReadToEnd();
// Nothing is closing the reader here! It'll keep an open
// file handle until it happens to be finalized

You should use something more like:

string data;
using (TextReader reader = File.OpenText("foo.txt"))
{
    data = reader.ReadToEnd();
}
// Use data here - the handle will have been closed for you

Or ideally, use the methods in File which do it all for you:

string text = File.ReadAllText("foo.txt");
末骤雨初歇 2024-12-15 02:25:01

检查您是否正在关闭文件流。如果没有,那么你就是在阻碍自己。

Check if you're closing stream to the file. If not then you're blocking yourself.

西瓜 2024-12-15 02:25:01

假设您已正确关闭最初用于打开和读取文件的流,要根据文件是否存在来创建、追加或失败,您应该在 FileStream 构造函数中使用 FileMode 参数。

一切都取决于您打开 FileStream 的方式,请参阅此处: FileStream 构造函数(String、FileMode )

如果您指定 FileMode 创建:

指定操作系统应创建一个新文件。如果
文件已存在,它将被覆盖。这需要
文件IOPermissionAccess.Write。 System.IO.FileMode.Create 等效
请求如果文件不存在,则使用CreateNew;
否则,使用截断。如果文件已存在但处于隐藏状态
文件时,会抛出 UnauthorizedAccessException。

Assuming that you have correctly closed the stream you used to open and read the file initially, to create, append or fail depending of file existence you should use the FileMode parameter in FileStream constructor.

Everything depends on the way you open the FileStream, see here: FileStream Constructor (String, FileMode)

if you specify FileMode Create:

Specifies that the operating system should create a new file. If the
file already exists, it will be overwritten. This requires
FileIOPermissionAccess.Write. System.IO.FileMode.Create is equivalent
to requesting that if the file does not exist, use CreateNew;
otherwise, use Truncate. If the file already exists but is a hidden
file, an UnauthorizedAccessException is thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文