C#错误的路径甚至@

时间:2017-11-06 00:07:28

标签: c# path uwp escaping directory

实际上,我只想计算文件夹中的数据。 不幸的是,路径无法正确识别。

            string tets = @"C:\Users\Tom\Downloads\AppBilder";
            [...]
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            [...]
            Ordnerquelle = folder.Path;


int directoryCount = Directory.GetDirectories(tets, " * ", SearchOption.AllDirectories).Length;
int fileCount = Directory.GetFiles(tets, "*", SearchOption.AllDirectories).Length;
count = directoryCount + fileCount;

调试器显示以下内容:

  • tets:“C:\\ Users \\ Tom \\ Downloads \\ AppBilder”
  • Ordnerquelle“C:\\ Users \\ Tom \\ Downloads \\ AppBilder”
  • 数:0

该文件夹不为空! 我错过了什么?

Screenshot

3 个答案:

答案 0 :(得分:1)

我说我的问题是UWP只能访问文件和文件夹 用FileOpenPicker或FolderPicker手动选择的用户。

在我的例子中,执行以下行:

StorageFolder folder = await folderPicker.PickSingleFolderAsync();
string tets = folder.Path;

之后,我打电话给Methode看起来像那样:

public static int docount();
{
int directoryCount = Directory.GetDirectories(tets, " * ", SearchOption.AllDirectories).Length;
int fileCount = Directory.GetFiles(tets, "*", SearchOption.AllDirectories).Length;
count = directoryCount + fileCount;
}

!!即使用户选择了路径,也会以字符串形式传递。!!

我的解决方案:

StorageFolder folder = await folderPicker.PickSingleFolderAsync();
int count = await Manager.docount(quelle);

public class Manager
{
public static async Task<int> ErstellltbilderlisteAsync(StorageFolder quelle)
        {
            int idcounter = 0;

            var query = quelle.CreateFileQuery();
            var fileList = await query.GetFilesAsync();
            foreach (StorageFile file in fileList)
            {
                idcounter++;

            }
            return idcounter;
}
}

我不知道这是否是最简单的方法,但我还在学习。 谢谢@Bite让我走上了正确的道路。

答案 1 :(得分:0)

我认为您的代码中存在小问题。您不应在搜索模式中提供空格。您使用" * "代替"*"

修改后的代码如下:

int directoryCount = Directory.GetDirectories(tets, "*", SearchOption.AllDirectories).Length;

int fileCount = Directory.GetFiles(tets, "*", SearchOption.AllDirectories).Length;

答案 2 :(得分:0)

在GetDirectories调用中删除searchPattern选项中的空格。

enter image description here

删除搜索字符串中的额外空格后的代码:

int directoryCount = Directory.GetDirectories(tets, "*", SearchOption.AllDirectories).Length;