Directory.CreateDirectory延迟问题? [英] Directory.CreateDirectory Latency Issue?

查看:150
本文介绍了Directory.CreateDirectory延迟问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个远程目录,然后向其中写入文件.每隔一段时间,应用程序都会在尝试写入文件时失败,并出现System.IO.DirectoryNotFoundException.

I'm trying to create a remote directory, and then write a file to it. Every great once in a while, the application fails with a System.IO.DirectoryNotFoundException while trying to write the file.

当我写文件时,我使用返回的DirectoryInfo对象来帮助创建文件路径,因此应用程序似乎认为目录已创建.但是,该目录不存在.

When I write the file, I'm using the returned DirectoryInfo object to help create the file path, so the application seems to think that the directory has been created. However, the directory does not exist.

在Windows完成创建目录之前,是否有可能尝试写入该目录?我认为Directory.CreateDirectory在此任务完成之前不会返回.

Is there any chance that I'm trying to write to the directory before Windows has finished creating it? I would think that the Directory.CreateDirectory would not return until this task was finished.

推荐答案

答案-是.当文件/目录创建滞后时,会出现这种情况.其他评论者建议的常规解决方案是使用重试超时.无论使用哪种文件功能,其行为都相同:Findfirst,CreateFile,WaitForSingleObject等.

Answer - Yes. The behavior, when file/directory creation is lagging is expected. Ordinary solution as proposed by other commenter is to use retries with some timeout. The behavior is the same whatever file functions are used: Findfirst, CreateFile, WaitForSingleObject etc.

另一种解决方案是使用Vista和更高版本Windows操作系统上提供的API的新事务功能.

Another solution will be to use new transactional functions of API found on Vista and later Windows OSes.

这个问题很棘手,在其他平台上开发并迁移到Windows的文件密集型项目的开发人员从未理解过,例如DOS/CMD脚本,SVN客户端,Cygwin,perl,各种Java应用程序,各种安装程序等.

The problem is nasty and was never understood by developers of file-intensive projects made on other platforms and moved to Windows: like DOS/CMD scripts, SVN clients, Cygwin, perl, various java applications, various installers etc.

这篇关于Directory.CreateDirectory延迟问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆