Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Is there a way to check if file is already open?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kimmo Laine - 31 May 2007 15:30 GMT
Hi!

Is there a way to check if file is already open/used by another process?

I know that i can do something like this to check it

   try {
       StreamWriter sw = new StreamWriter(filename);
       sw.Close();
   } catch (IOException) {
       // File is already open...
   }

but I would rather use some method then try to open the file in write mode.

thx
Kimmo
Larry Smith - 31 May 2007 18:06 GMT
> Hi!
>
[quoted text clipped - 11 lines]
> but I would rather use some method then try to open the file in write
> mode.

It's transitory information which you shouldn't normally rely on. A file
that's opened/closed one moment could be closed/opened the next. The safest
route is to simply open the file with the appropriate "FileShare" access.
See this enumeration for details.
Peter Duniho - 31 May 2007 18:38 GMT
> Is there a way to check if file is already open/used by another process?
>
[quoted text clipped - 3 lines]
> but I would rather use some method then try to open the file in write  
> mode.

Why do you want this information?

Note that if you do not yourself open and lock the file, then checking  
whether the file is open at any given moment in time is pointless.  A  
split second after you make the check, the state could change.

If you need to open the file yourself, then just try to open it.  If you  
don't need to open the file yourself, then there is not usually really any  
use in knowing whether the file is already open or used by another process.

(One exception would be if you are trying to write a tool similar to the  
Sysinternals FileMon utility, but the kind of things that tool does  
requires much lower-level coding than .NET provides).

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.