使用IsolatedStorage,创建目录之前是否需要检查目录是否存在?
我正在开发一款 Windows Phone 7 应用程序,我想知道是否有人对我是否必须在创建目录之前检查目录是否存在有明确的答案,以及这样做/不这样做的优点/缺点是什么。据我所知,从单步执行我的代码来看,以下两个代码块以相同的方式工作:
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
if (!appStorage.DirectoryExists(sDirectory))
{
appStorage.CreateDirectory(sDirectory);
}
}
?
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
appStorage.CreateDirectory(sDirectory);
}
使用第二个代码块安全吗 如果目录已经存在,它似乎不会抛出异常,并且似乎也不会保留目录的内容。
I'm working on a Windows Phone 7 app, and I was wondering whether anyone had a definitive answer on whether or not I have to check if a directory exists before creating one, and what the advantages/disadvantages of doing/not doing so are. As far as I can tell, from stepping through my code, the following two blocks of code work in the same manner:
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
if (!appStorage.DirectoryExists(sDirectory))
{
appStorage.CreateDirectory(sDirectory);
}
}
and
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
appStorage.CreateDirectory(sDirectory);
}
Is it safe to use the second block of code? It didn't seem to throw an exception if the directory already existed, and also seemed to leave the contents of the directory alone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IsolatedStorageFile.CreateDirectory
将在内部调用Directory.CreateDirectory
。Directory.CreateDirectory
的文档指出:换句话说,您不需要检查该目录是否存在。该方法已经为您做到了这一点。
The
IsolatedStorageFile.CreateDirectory
will callDirectory.CreateDirectory
internally. The documentation ofDirectory.CreateDirectory
states:In other words, you don't need to check if that directory exists. The method already does that for you.
我怀疑 CreateDirectrory 内部正在检查目录是否已存在或正在吞咽异常。不管怎样,事先显式调用
DirectoryExists
可能会带来一点性能上的好处。确定测试的方法是通过创建大量目录来对两种方法的性能进行基准测试。 (如果您尝试这样做,请注意父目录中的目录不能超过 16k,并且目录深度不能超过 18 个(我认为)。)
最好的做法是明确说明您的目录正在做。我希望任何其他查看代码的开发人员都会问您在创建目录之前没有测试其存在性。特别是如果多次调用此代码。如果您测试并发现性能没有差异,我建议在代码中添加注释来说明这一点。
I suspect that internally
CreateDirectrory
is doing a check if the directory already exists or is swallowing an exception. Either way, there is probably a small performance benefit to be had from callingDirectoryExists
explicitly before hand.The way to test for sure would be to benchmark performance of the 2 methods with creating a large number of directories. (If you try this, be aware that you can't have more than 16k directories in a parent directory and you can't go more than 18 (I think) directories deep.)
It's better practice to be explicit about what you're doing. I would hope that any other developer who looked at the code would ask you you weren't testing for existence before creating a directory. Especially if this code was called many times. If you test and find no difference in performance, I'd recommend a comment in the code to state this.