I want to open a file for read in exclusive mode, and if the file is already opened by some process/thread else, I want to receive an exception. I tried the following code, but not working, even if I opened the foo.txt, I still can reach the Console.WriteLine statement. Any ideas?
static void Main(string[] args)
{
using (Stream iStream = File.Open("c:\software\code.txt", FileMode.Open,
FileAccess.Read, FileShare.None))
{
Console.WriteLine ("I am here");
}
return;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…