try {} catch {} not working
-
Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:
public static bool FileInUse(string path
{
bool blnReturn = false;
FileInfo file = new FileInfo(path);
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
fs.Close();
}
catch
{
blnReturn = true;
}
return blnReturn;
}
); But, instead of returning a value, the programm is crashing in
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
What is wrong here?
-
Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:
public static bool FileInUse(string path
{
bool blnReturn = false;
FileInfo file = new FileInfo(path);
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
fs.Close();
}
catch
{
blnReturn = true;
}
return blnReturn;
}
); But, instead of returning a value, the programm is crashing in
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
What is wrong here?
Are you running in a debugger? They will often notify you of exceptions even inside a catch. If you run this application on its own that should work, though it has a couple of issues (you will create the file if it doesn't exist, and you should be catching the specific exception you are looking for if you are just trying to find if the file is locked by another app).
-
Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:
public static bool FileInUse(string path
{
bool blnReturn = false;
FileInfo file = new FileInfo(path);
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
fs.Close();
}
catch
{
blnReturn = true;
}
return blnReturn;
}
); But, instead of returning a value, the programm is crashing in
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
What is wrong here?
I use this function,
private static bool FileInUse(string path)
{
FileInfo file = new FileInfo(path);
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException ex)
{
//MessageBox.Show(ex.Message);
//File in use
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}although it will only return true if the file is Locked by another application(or yours). If the other application has opened the file without locking it will return false. You should be careful with your implementation as if the file is not found it would create one.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
Are you running in a debugger? They will often notify you of exceptions even inside a catch. If you run this application on its own that should work, though it has a couple of issues (you will create the file if it doesn't exist, and you should be catching the specific exception you are looking for if you are just trying to find if the file is locked by another app).
-
I use this function,
private static bool FileInUse(string path)
{
FileInfo file = new FileInfo(path);
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException ex)
{
//MessageBox.Show(ex.Message);
//File in use
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}although it will only return true if the file is Locked by another application(or yours). If the other application has opened the file without locking it will return false. You should be careful with your implementation as if the file is not found it would create one.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
Thanks for the answer. I will change "FileMode.OpenOrCreate" to "FileMode.Open" as you said. The oly problem that was causing the program crash was I was running it in debug mode. I have runned it by its own as BobJanova advised, and now it is working fine.
-
Thanks a lot! I runned the app by its own as you told, and that worked!
modified on Thursday, April 21, 2011 5:38 AM
Welcome. You should be able to press Continue or similar (it's F5 in the debuggers I know) when you get an exception breakpoint inside a try when running in the debugger, by the way. You can also set your debugger not to notify you of caught exceptions.
-
Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:
public static bool FileInUse(string path
{
bool blnReturn = false;
FileInfo file = new FileInfo(path);
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
fs.Close();
}
catch
{
blnReturn = true;
}
return blnReturn;
}
); But, instead of returning a value, the programm is crashing in
fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
What is wrong here?
Unfortunately, your method and the method below does not work. If you place a lock on the file you have to wait for the O.S. to release the file. Thus a call FileInUse that returns true can still fail on the file open. The only way is to open the file and lock the file and return that lock.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost