【问题标题】:c# error when trying to create or write file [duplicate]尝试创建或写入文件时出现c#错误[重复]
【发布时间】:2021-07-22 11:50:44
【问题描述】:

这个使用 Windows 窗体的 C# 代码给了我一个错误,说该文件被另一个进程使用,尽管我没有在任何地方打开它。

private void browseBtn2_Click(object sender, EventArgs e)
{
    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
    dialog.InitialDirectory = "C:\\Users";
    dialog.IsFolderPicker = true;
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        MessageBox.Show("You selected: " + dialog.FileName);
        this.outputPathText.Text = dialog.FileName;
    }
}

private void genBtn_Click(object sender, EventArgs e)
{
    string path = outputPathText.Text+@"\test.txt";
    TextWriter tw = new StreamWriter(path); //gives me the error here -_-
    tw.WriteLine("The very first line!");
    tw.Close();

    genSuccess.Visible = true;
    wait(2000);
    genSuccess.Visible = false;
}

【问题讨论】:

  • 您在browseBtn2_Click 函数中将文件名设置为this.outputPathText.Text,并在genBtn_Click 函数中将其引用为outputPathText.Text。你在genBtn_Click函数中试过this.outputPathText.Text吗?
  • 在命令提示符下运行:openfiles /query /fo csv /v > c:\temp\openfiles.txt,然后在openfiles.txt 文件中查找test.txt,查看当前锁定了哪个进程。
  • 视情况而定,您要么需要弄清楚是哪个进程锁定了文件(参见第一个副本),要么您需要以可以容纳使用同一文件的其他进程的方式访问文件(见其他副本)。请注意,只有后者才是真正的 Stack Overflow 问题。如果您只想知道哪个进程打开了文件,那是一个“通用计算硬件和软件”问题,属于其他地方(例如 superuser.com)。

标签: c# winforms


【解决方案1】:

它可能被程序的旧实例或某些故障进程打开。我建议检查任务管理器并重新启动资源管理器。在 MacOS 上,我遇到了类似的问题,不得不重新启动计算机。

【讨论】:

  • 我没有在任务管理器中找到我的程序,但在资源监视器中有带有程序名称的进程。当我尝试关闭它时,它显示“拒绝访问”。我也重启了资源管理器
  • 您应该尝试重新启动计算机,看看是否能解决问题。某些进程可能会出现错误并且几乎不可能被杀死,因为它们正在使用一些内核资源。
  • 好的,我会试试的。谢谢。
【解决方案2】:

目前不确定这是否是您的问题的原因,但您的问题可能是因为您没有 Dispose 实现 IDisposable 的对象的习惯。

如果一个对象实现了 IDisposable,那么该对象的设计者认为它拥有稀缺资源。应该尽快处理掉。

我看到的一个问题是,当人们忘记 Dispose 对文件的访问权限时,您无法删除它,因为它仍在使用中。

string bitmapFileName = "MyTestFile.bmp";
Bitmap testBitmap = new Bitmap(bitmapFileName);

... // do something with the bitmap. Or not, if you don't want to

// after a while you don't need the bitmap anymore
testBitmap = null;
System.IO.File.Delete(testBitMap);   // Exception! File still in use

尝试让实现 IDisposable 的 Dispose 对象成为一种习惯:

string fileName = outputPathText.Text+@"\test.txt";
using (TextWriter textWriter = File.Create(fileName))
{
    textWriter.WriteLine("The very first line!");
}

就是这样:无需刷新或关闭,textWriter 的 Dispose 将为您完成。

同样:

using (var bmp = new Bitmap(testBitmap))
{
    ... // do something with it, or not if you don't want to
}

// now the bitmap can be deleted
File.Delete(testBitmap);

我不熟悉 CommonOpenFileDialog,也不容易找到 Microsoft 的描述,但如果实现 IDisposable,就像每个 Form 一样,它也应该被释放:

using (var dialog = new CommonOpenFileDialog())
{
    dialog.InitialDirectory = "C:\\Users";
    dialog.IsFolderPicker = true;
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        MessageBox.Show("You selected: " + dialog.FileName);
        this.outputPathText.Text = dialog.FileName;
    }
}

Dispose 将清理 CommonOpenFileDialog 中的所有内容并释放所有使用的资源。

【讨论】:

  • 缺少Dispose 很容易导致此类问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 2011-02-25
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
相关资源
最近更新 更多