如何获取子目录文件? [英] how to get the subdirectories files?

查看:91
本文介绍了如何获取子目录文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string path =D:/'+ variable +'/+ variable2;

string [] files = Directory.Getfiles(path);

这段代码生成那个输出:D:/ folder / folder2 \\file

但这个错误路径不支持我想要这个路径

输出:D:/文件夹/ fodler2 /文件

plz帮助我........谢谢

string path="D:/'"+variable+"'/"+variable2";
string[] files=Directory.Getfiles(path);
this code generate thats output: D:/folder/folder2\\file
but this got error path not supported and i want this path
output: D:/folder/fodler2/file
plz help me........thanks

推荐答案

开始通过查看 Path.Combine [ ^ ] - 它比字符串连接更好地构建路径,因为它智能地查看数据并合理化它。



Start by looking at Path.Combine[^] - it builds paths better than string concatenation because it intelligently looks at the data and "rationalises" it.

string variable = @"Temp\";
string variable2 = @"wzdd89\";
string path = "D:/'" + variable + "'/" + variable2;
string betterPath = Path.Combine(@"D:\", variable, variable2);
string[] files = Directory.GetFiles(betterPath);


Windows文件路径使用\ not /并将\放在ac#字符串中,您需要使用另一个\来转义它,如下所示:

Windows file paths use \ not / and to put a \ in a c# string you need to escape it with another \ like the following:
string path="D:\\"+variable+"\\"+variable2";


string variable = @"'"+foldername+"'\"";
                                 string variable2 = @"'" + files2[i] + "'\"";
                                 string path = "D:/'" + variable + "'/" + variable2;
                                 string betterPath = Path.Combine(@"D:\", variable, variable2);
                                 string[] files = Directory.GetFiles(betterPath);





i使用此但错误在路径中是非法字符



i use this but error come illegal character in path


这篇关于如何获取子目录文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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