-1

This code in C# using Windows Forms gives me an error that says that the file is used by another Process altough i do not have it open anywhere.

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;
}
uvr
  • 515
  • 4
  • 12
  • You have the filename being set to `this.outputPathText.Text` in the `browseBtn2_Click` function, and being referenced as `outputPathText.Text` in the `genBtn_Click` function. Have you tried `this.outputPathText.Text` in the `genBtn_Click` function? – Will Walsh Apr 29 '21 at 06:13
  • In a command prompt run: `openfiles /query /fo csv /v > c:\temp\openfiles.txt` and then look for `test.txt` in the `openfiles.txt` file to see which process has it currently locked. – Loathing Apr 29 '21 at 06:57
  • Depending on the situation, you either need to figure out what process has the file locked (see first duplicate) or you need to access the file in a way that can accommodate other processes using the same file (see other duplicates). Note that only the latter is really a Stack Overflow question. If you just want to know which process has the file open, that's a "general computing hardware and software" question and belongs elsewhere (e.g. superuser.com). – Peter Duniho Apr 29 '21 at 07:30

2 Answers2

1

Not sure if this is the cause of your problem right now, but your problems might be caused because you don't have the habit to Dispose objects that implement IDisposable.

If an object implements IDisposable, then the designer of the object thought that it holds scarce resources. It should be Disposed as soon as possible.

One of the problems I see, when people forget to Dispose the access to a file, is that you can't delete it, because it is still in use.

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

Try to make it a habit to Dispose objects that implement IDisposable:

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

That's it: no need to Flush nor Close, the Dispose of the textWriter will do that for you.

Similarly:

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);

I'm not familiar with the CommonOpenFileDialog, and couldn't easily find the Microsoft description for it, but if if implements IDisposable, like every Form does, it should also be disposed:

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 will clean up everything in the CommonOpenFileDialog and free up all resources used .

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0

It could be open by an older instance of the program or some malfunctioning process. I would recommend checking task manager and restarting explorer. On MacOS, I've encountered a similar issue and had to restart the computer.

lizardpeter
  • 89
  • 1
  • 7
  • I didn't find my program in the task manager but in the resource monitor there are processes with the name of the program. When i try to close it it says "Access denied". I have also restarted the Explorer – Marceloni Apr 29 '21 at 06:16
  • You should try to restart the computer to see if that resolves the issue. Some processes can become bugged and almost impossible to kill because they're using some kernel resource. – lizardpeter Apr 29 '21 at 06:20
  • Okay, i will try. Thank you. – Marceloni Apr 29 '21 at 06:22