C# 递归地将目录名追加到字符串

C# 递归地将目录名追加到字符串,c#,.net,algorithm,exception-handling,io,C#,.net,Algorithm,Exception Handling,Io,我有个大问题,好吧,问题是: 我试图从目录中获取文件信息,以便在listview中列出它。 当我使用该方法递归搜索文件时: private void Get_Files(string path) { DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fi = di.GetFiles(); foreach (FileInfo Info in fi) {

我有个大问题,好吧,问题是: 我试图从目录中获取文件信息,以便在listview中列出它。 当我使用该方法递归搜索文件时:

    private void Get_Files(string path)
    {
        DirectoryInfo di = new DirectoryInfo(path);

        FileInfo[] fi = di.GetFiles();

        foreach (FileInfo Info in fi)
        {
            try
            {
                Files.Add(Info.FullName);
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        foreach (DirectoryInfo DInfo in di.GetDirectories())
        {
            Get_Files(DInfo.FullName);
        }
    }
有时路径长度超过260个字符,因此我得到了该错误: 路径太长,不应该超过260个字符,我在互联网上搜索过,人们说它没有解决方案,但我自己找到了一个解决方案。 解决方案:创建一个字符串并将该路径的每个路径附加到该字符串中,这样在将整个路径保存到字符串中时就不会出现该错误。 把它想象成把路径分开,把每一个片段都放在绳子上。 因此,以下是我想到的解决方案:

    List<string> Files = new List<string>();

    string completepath = string.Empty;
    string current_dire_name = string.Empty;

    private void Get_Files(string path)
    {
        DirectoryInfo di = new DirectoryInfo(path);

        FileInfo[] fi = di.GetFiles();

        foreach (FileInfo Info in fi)
        {
            try
            {
                completepath += "\\" + Info.Name;
                Files.Add(completepath);
                string remove_file_name = completepath;
                remove_file_name = remove_file_name.Replace("\\" + Info.Name, "");
                completepath = remove_file_name;
            }
            catch(Exception ee)
            {   
                if(DialogResult.Yes == MessageBox.Show("Error at the Get_Files Method and Error message :\n\n" + ee.Message + "\n\nQuit Application now ?","",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
                {
                    Environment.Exit(0);
                }
            }
        }

        foreach (DirectoryInfo DInfo in di.GetDirectories())
        {
            string remove_folder_name = completepath;
            remove_folder_name = remove_folder_name.Replace("\\" + current_dire_name, "");
            completepath = remove_folder_name;

            current_dire_name = DInfo.Name;
            completepath += "\\" + DInfo.Name;
            Get_Files(DInfo.FullName);
        }
    }
List Files=newlist();
string completepath=string.Empty;
string current\u dire\u name=string.Empty;
私有void Get_文件(字符串路径)
{
DirectoryInfo di=新的DirectoryInfo(路径);
FileInfo[]fi=di.GetFiles();
foreach(fi中的文件信息)
{
尝试
{
completepath+=“\\”+信息名称;
添加(completepath);
string remove\u file\u name=completepath;
remove\u file\u name=remove\u file\u name.Replace(“\\”+Info.name,”);
completepath=删除文件名;
}
捕获(异常ee)
{   
if(DialogResult.Yes==MessageBox.Show(“Get_Files方法出错,错误消息:\n\n”+ee.message+“\n\n立即退出应用程序?”,“”,MessageBoxButtons.YesNo,MessageBoxIcon.Question))
{
环境。退出(0);
}
}
}
foreach(di.GetDirectories()中的DirectoryInfo DInfo)
{
string remove\u folder\u name=completepath;
移除文件夹名称=移除文件夹名称。替换(“\\”+当前文件夹名称“”);
completepath=删除文件夹名称;
当前名称=DInfo.name;
completepath+=“\\”+DInfo.Name;
获取_文件(DInfo.FullName);
}
}
好的,那个方法救了我,但它生成了错误的路径,我的意思是有些地方不正确,假设路径应该是:C:\Folder1\Folder2\Folder3\file.txt 生成的路径是:C:\Folder1\file.txt,类似于。。。。 我知道我使用的方法有一些错误,尤其是递归追加


我希望有人能和我一起解决这个问题,这样人们就可以避免长路径异常。

您正在寻找的,它在Windows API中使用
\\?\
前缀,以完全避免限制。

这是因为您的方法是错误的,而且无论如何也帮不上忙。方法有一些错误,我知道,这就是我想要解决的问题,也许如果有人分享一个更好的算法,那会有帮助,因为没有像这样的解决办法<代码>获取*()将在大于260个字符的字符串上失败,无论您如何构建字符串。该库应该避免长路径异常吗?实际上,我只想获得文件的详细信息,如大小和完整路径。您给我的库仅用于.net 4,用于IO复制/移动/删除/打开操作,目的是获取文件信息。但我在这里找到了另一个解决这个问题的图书馆: