开发者

How to set a time-out for File.Create method on network drive?

开发者 https://www.devze.com 2023-02-19 16:44 出处:网络
I have a requirement in my app开发者_开发问答lication where I need to create the file in a network drive,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消