Distinguishing between "file in use" and other exceptions in .NET -


I am reading a file from a directory, I should handle that situation where my application tries to pick up such a file Which is still being written by any other process, on which I have no control.

I am assuming that by trying to open the file, which is still being written to do something else, I am going to get an exception. Now, I need to know if there is something wrong with the file or not, in that case I take it to another directory or else the other process is still in the file, in that case I will be again in second iteration Want to try reading the directory?

Does anybody know how I can differentiate between those two exceptions?

You can use the Marshal.GetHRForException () method to recover the original window error IOException has been raised. An example:

  Static filestream TryOpen (string path, int maxAttempts = 10, int interval = 1000) {for (int try = 0;; attempts ++) {try new filestream (path, FileMode.Open, FileAccess.Read, FileShare.None); } Hold (IOException pre) {var err = System.Runtime.InteropServices.Marshal.GETHRForException (former) & amp; 0xffff; Throw (try & lt; maxAttempts & error! = 32); } System.Threading.Thread.Sleep (interval); }}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -