I have a requirement in my app开发者_开发问答lication where I need to create the file in a network drive, but when file creation in progess if network disconnected application hangs for a while and throws an exception.
Is there any way we can set time-out for File.Create
?
Something like that can be used to implement time-out manually:
var fileCreatingThread = new Thread(...);
fileCreatingThread.Start();
if (fileCreatingThread.Join(TimeSpan.FromSeconds(5)) { /* Worked correctly */}
else
{
// time-out
}
I don't know of any way to configure the network timeout for File.Create(). It seems likely that this is built into the framework or more likely the operating system itself.
Can you try to establish a network connection before you create the file? Maybe ping the target or open a socket? If this fails, you know not to try to create the file. If it succeeds, there's a good chance the file creation will go through unless there's a security issue.
精彩评论