I saw both of these questions:
Is there a way to check if a file is in use?
And neither of them provided me all of the info that I needed, and I needed more clarification on some of the answers, but the questions were several years old, so I wasn't sure if I should try to get responses from there at this point.
So I posted a new question. Something like
public string myFile;
myFile = @"C:\somepath\somefile.pdf";
if (myFile isinuseorwhatever)
{
MessageBox.Show("File is in use!! Close it and try again");
return;
}
else
{
MessageBox.Show("That worked. Good job!")
//Do Stuff and lots of lines of stuff.
}
I can do this using exception handling, but the issue is that I need to do the check before I run the many lines of code.
I have a feeling that I need to make a class to check it, then run that class. To be honest, I'm pretty new to coding so I'm not 100% clear on how classes work.
I know about using try
and catch
, but that won't work here because the exception is happening in the last few lines of code in the try
block, so all of that stuff will happen before it hits the exception. Like, this program copies a file, renames it, moves it into a different directory, and then deletes the original, which is the last thing it does. If a user has the file open, it will do all of that but then throw an exception when it tries to delete. I need it to throw the exception BEFORE it copies, renames, moves and so on.