简体   繁体   中英

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);
                }
            }
        }
    }
}

I'm getting the error on the line:

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

Could not find file 'D:\Csharp Projects\Get Messages\bin\Debug\Source0.html

The path from the error message D:\Csharp Projects\Get Messages\bin\Debug isn't the path or even a subpath from the one I provided in the source, eg D:\Csharp Projects\Get Messages\Sources

Why isn't the given path used?

Solution is to use file.FullName:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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