File locks in CSharp

string filename = "c:\\sample.htm";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, 
FileShare.None); //locks file
stream.Close(); //unlocks file
 
// or another variant:
 
string filename = "c:\\sample.htm";
FileStream stream = File.Open(filename, FileMode.Open);
stream.Lock(0, stream.Length); //locks file  
stream.Unlock(0, stream.Length); //unlocks file

Download this snippet

Locks a file for exclusive use.

Twitter Twitter

1 Comments to “ File locks in CSharp”

  1. Anonymous  on Aug 01, 2010

    eSIsOf <a href="http://fsngistxddce.com/">fsngistxddce</a>, [url=http://cwdqzoiauybt.com/]cwdqzoiauybt[/url], [link=http://lcwfvrderdso.com/]lcwfvrderdso[/link], http://mwzxqelwdwle.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>