G
Guest
Hello!
Using .NET v2.0.50727, I've attached some sample code that creates a file in
a network path (test.er-1):
- The file is created at a network path.
- The user disconnects his/her network cable.
- The file is attempted to write to.
- The user reconnects his/her network cable.
- The file is closed - WHICH fails and results in the exception "The handle
is invalid"
This results in that the filelock remain active until the process is
terminated... Any suggestions on how to close the file in the same scenario?
Best Regards,
Fredrik Johansson
Using .NET v2.0.50727, I've attached some sample code that creates a file in
a network path (test.er-1):
- The file is created at a network path.
- The user disconnects his/her network cable.
- The file is attempted to write to.
- The user reconnects his/her network cable.
- The file is closed - WHICH fails and results in the exception "The handle
is invalid"
This results in that the filelock remain active until the process is
terminated... Any suggestions on how to close the file in the same scenario?
Best Regards,
Fredrik Johansson
Code:
private FileStream fs1;
public void Runme() {
lock (this) {
string networkFile = @"\\anyserver\anyshare\test.er-";
// open the file
Console.Write("Creating Files (OK expected)...");
try {
fs1 = new FileStream(networkFile + "1",
FileMode.CreateNew, FileAccess.Write, FileShare.Read);
Console.WriteLine("OK");
} catch (Exception e) {
Console.WriteLine("FAILED\r\nThe test can not be
performed. Please modify the networkFile parameter!");
Console.WriteLine("\r\nPress ENTER to exit...");
Console.ReadLine();
return;
}
// wait for user to disconnect
Console.Write("Please disconnect from your network, press
ENTER to continue...");
Console.ReadLine();
// try to write some data - this will fail
try {
Console.Write("Attempting to Write (FAILED expected)...");
byte[] ba = Encoding.Default.GetBytes("Hello World");
fs1.Write(ba, 0, ba.Length);
fs1.Flush();
Console.WriteLine("OK - Did you really disconnect your
drive?");
} catch (Exception e) {
Console.WriteLine("FAILED");
}
Console.Write("Please connect to your network, press ENTER
to continue...");
Console.ReadLine();
// try to close the file - this will fail
try {
Console.Write("Closing File (this WILL fail)...");
fs1.Close();
fs1 = null;
Console.WriteLine("OK - Both files are supposidly
closed...");
} catch (Exception e) {
//
===================================================================
// break here "The handle is invalid" - but the filelock
is still on?!
Console.WriteLine("FAILED");
}
Console.WriteLine("\r\nPress ENTER to exit...");
Console.ReadLine();
}
}