为什么Directory.Exists返回true但实际上路径不存在 [英] Why Directory.Exists returns true but actually the path is not existing

查看:517
本文介绍了为什么Directory.Exists返回true但实际上路径不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2017将我的桌面应用程序更改为UWP作为visual-studio-2017-update-4-makes-easy-modernize-desktop-application-make-store-ready /的指南。 / p>

根据en-us / library / mt185496.aspx,UWP支持System.IO命名空间。


我们知道,用户的数据不应该写在已安装的文件夹中,而应该位于"Root Drive:\ Users \ username \ AppData \"中漫游" ;.我遵循这条规则。但我遇到了桌面应用程序中从未发生的问题。


        private static string EnsureDirectory(string directoryPath)

        {

            if(!Directory.Exists(directoryPath))

            {

                Directory.CreateDirectory(directoryPath);

            }
            return directoryPath;

        }


代码很简单。我想在访问之前确保该目录已经存在。目录路径不存在,但Directory.Exists(directoryPath)返回TRUE。我试图省略检查(注释掉检查行)并转到Directory.CreateDirectory(directoryPath),
执行方法时没有任何错误信息,但之后,不创建文件夹。


如何处理这个问题?我是否必须引用Windows.Storage.dll并重写与文件/目录操作相关的所有方法?



解决方案

您应该在UW​​P应用程序中使用 Windows.Storage ,并且只能在访问的路径中创建文件夹和文件,而不是您想要的每个路径(在Windows桌面中,您可以随时随地使用文件夹选择器。


您可以使用此方法在 LocalState 文件夹中创建一个文件夹,如下所示:

 private async Task< string> EnsureDirectory(string folderName)
{
var file = await ApplicationData.Current.LocalFolder.TryGetItemAsync(folderName);
if(file!= null)
await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync(folderName);
返回folderName;
}


I am using Visual Studio 2017 to change my desktop application into UWP as guide in visual-studio-2017-update-4-makes-easy-modernize-desktop-application-make-store-ready/.

As per en-us/library/mt185496.aspx, System.IO namespaces are supported in UWP.

As we know, the user's data should not be written in installed folder and they should be in "Root Drive:\Users\username\AppData\Roaming". I followed this rule. But I encountered an issue never happened in desktop application.

        private static string EnsureDirectory(string directoryPath)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            return directoryPath;
        }

The code is simple. I want to make sure the directory already existed before I access it. the directory path is not exist but Directory.Exists(directoryPath) returns TRUE. And I tried to omit the check (comment out the checking line) and go to Directory.CreateDirectory(directoryPath), the method is executed without any error information, but after that, the folder is not created.

How to deal with this issue? Do I must reference Windows.Storage.dll and rewrite all methods related to file/directory operations?

解决方案

You should use Windows.Storage in UWP application and you can create folder and file only in accessed path, not each path you want (in Windows Desktop you can use folder picker from anywhere).

You can create a folder in LocalState folder surly with this method which see below:

        private async Task<string> EnsureDirectory(string folderName)
        {
            var file = await ApplicationData.Current.LocalFolder.TryGetItemAsync(folderName);
            if (file != null)
                await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync(folderName);
            return folderName;
        }


这篇关于为什么Directory.Exists返回true但实际上路径不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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