gpt4 book ai didi

c# - 使用 IsolatedStorage,是否需要在创建目录之前检查目录是否存在?

转载 作者:太空狗 更新时间:2023-10-30 01:10:54 24 4
gpt4 key购买 nike

我正在开发 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);

}

使用第二段代码安全吗?如果目录已经存在,它似乎不会抛出异常,而且似乎也只保留目录的内容。

最佳答案

IsolatedStorageFile.CreateDirectory 将在内部调用 Directory.CreateDirectorydocumentation Directory.CreateDirectory 状态:

If the directory already exists, this method does nothing.

换句话说,您不需要检查该目录是否存在。该方法已经为您做到了。

关于c# - 使用 IsolatedStorage,是否需要在创建目录之前检查目录是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com