【问题标题】:Why some paths with double back slash throw Could not find a part of the path exception?为什么某些带有双反斜杠的路径抛出找不到路径异常的一部分?
【发布时间】:2020-05-27 18:19:06
【问题描述】:

例如,这个路径和文件没有例外:“E:\Samsung Galaxy S9\Danny Backup\Recovered data 02-10 18_32_36\1 (D) NTFS\C-Sharp\Download File\Downloading-File-Project -Version-006\Image-Downloader\Downloading-File-Project-Version-005\Download File\Downloading File\Downloading File\About.cs"

但随后在文件的另一个路径上抛出异常找不到路径的一部分:

找不到路径的一部分“E:\Samsung Galaxy S9\Danny Backup\Recovered data 02-10 18_32_36\1 (D) NTFS\C-Sharp\Download File\Downloading-File-Project-Version- 006\Image-Downloader\Downloading-File-Project-Version-005\Download File\Downloading File\Downloading File\FileDownload_Test.Designer.cs"

我在文件资源管理器中检查了移动到此路径的手册,并且该文件存在,我可以使用记事本编辑该文件。

我还尝试在文件资源管理器中找到带有双反斜杠的路径,但没有找到路径:

但是如果我删除一个反斜杠,它会找到它会到达的路径。 如果我尝试访问包含文件名的路径,它会在文件资源管理器中向我显示路径长于 269 个字符的消息。

查看我看到的异常的 StackTrace 时发现:

在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath、Boolean checkHost) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问,FileShare 共享,Int32 bufferSize,FileOptions 选项,字符串 msgPath,布尔 bFromProxy,布尔 useLongPath,布尔 checkHost) 在 System.IO.StreamReader..ctor(字符串路径,编码编码,布尔检测编码FromByteOrderMarks,Int32 bufferSize,布尔检查主机) 在 System.IO.File.InternalReadAllText(字符串路径,编码编码,布尔检查主机) 在 System.IO.File.ReadAllText(字符串路径) 在 E:\Samsung Galaxy S9\Danny Backup\Recovered data 02-10 18_32_36\1 (D) NTFS\C- 中的 Search_Text_In_Files.Form1.DirSearch(String rootDirectory, String filesExtension, String[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e) Sharp\Search_Text_In_Files\Search_Text_In_Files\Search_Text_In_Files\Form1.cs:第 275 行

第 275 行是:

var tempFR = File.ReadAllText(file);

我不明白 FileDownload_Test.Designer.cs 文件有什么问题,为什么第一个文件 About.cs 很好,而第二个文件出现异常。这两个文件都存在并且可以编辑/打开。

我很困惑。也许在文件资源管理器中我可以访问文件但在程序中由于某种原因它不能?

int numberofdirs = 0;
        void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
        {
            List<string> filePathList = new List<string>();
            int numberoffiles = 0;
            try
            {
                filePathList = SearchingCore.SearchAccessibleFilesNoDistinct(rootDirectory, null, worker, e,_isbackgroundbusy,
                    textBoxSetFileExtensions, lblretrievedfiles,lblrestrictedfiles).ToList();
            }
            catch (Exception err)
            {

            }
            lblphases.Invoke((MethodInvoker)delegate
                    {
                        lblphases.Text = "Phase 2: Searching in files";
                    });
            MyProgress myp = new MyProgress();
            myp.Report4 = filePathList.Count.ToString();
            foreach (string file in filePathList)
            {
                try
                {
                    var tempFR = File.ReadAllText(file);

                    _isbackgroundbusy.WaitOne();
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        return;
                    }

                    bool reportedFile = false;

                    for (int i = 0; i < textToSearch.Length; i++)
                    {
                        if (tempFR.IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            if (!reportedFile)
                            {
                                numberoffiles++;

                                myp.Report1 = file;
                                myp.Report2 = numberoffiles.ToString();
                                myp.Report3 = textToSearch[i];
                                myp.Report5 = FindWordsWithCountRegex(tempFR, textToSearch).ToString();
                                backgroundWorker1.ReportProgress(0, myp);
                                reportedFile = true;
                            }
                        }
                    }
                    numberofdirs++;
                    lblsearchedfiles.Invoke((MethodInvoker)delegate
                    {
                        lblsearchedfiles.Text = string.Format("{0}/{1}", numberofdirs, myp.Report4);
                        lblsearchedfiles.Visible = true;
                    });
                }
                catch (Exception err)
                {
                    string fff = "";
                }
            }
        }

【问题讨论】:

标签: c# winforms


【解决方案1】:

About.cs 文件的完整路径为 244 个字符。 FileDownload_Test.Designer.cs 文件长度为 265 个字符。

您的 File.ReadAllText() 路径不得超过 260 个字符。

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 1970-01-01
    • 2020-11-25
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2013-01-22
    相关资源
    最近更新 更多