繁体   English   中英

为什么我收到错误找不到其他文件夹中的文件名而不是我给的那个?

[英]Why I'm getting error can't find file name in other folder then the one I gave?

private void GetMessages()
{
    DirectoryInfo d = new DirectoryInfo(@"D:\Csharp Projects\Get Messages\Sources");//Assuming Test is your Folder
    FileInfo[] Files = d.GetFiles("*.html"); //Getting Text files

    foreach (FileInfo file in Files)
    {
        using(StreamReader reader = new StreamReader(file.Name))
        {
            string line;

            while((line = reader.ReadLine()) != null)
            {
                if(line.Contains("msg-bullet"))
                {
                    int index = line.IndexOf("<");
                    int index1 = line.IndexOf(">");

                    string result = line.Substring(index, 4);
                }
            }
        }
    }
}

我在线收到错误消息:

using(StreamReader reader = new StreamReader(file.Name))

找不到文件 'D:\Csharp Projects\Get Messages\bin\Debug\Source0.html

错误消息D:\Csharp Projects\Get Messages\bin\Debug的路径不是我在源代码中提供的路径甚至是子路径,例如D:\Csharp Projects\Get Messages\Sources

为什么不使用给定的路径?

解决方案是使用 file.FullName:

using (StreamReader reader = new StreamReader(file.FullName))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM