Search code examples
c#system.io.file

C#: System.IO.DirectoryNotFoundException: 'Could not find a part of the path


I am getting an error as shown in the title. I have no idea why im getting this or as to how im supposed to fix this. Visual Studio is giving me the error shown in the title on this line: var f = System.IO.File.OpenRead(fil1);

class Program {
    static void Main(string[] args) {

        Stopwatch sw = Stopwatch.StartNew();

        string fil1 = "C:\Users\mariu\Desktop\Jobboppgave\CaseConsoleApp\Prisfile.txt";

        ***var f = System.IO.File.OpenRead(fil1);***
        int length = (int)(new System.IO.FileInfo(fil1).Length);

Solution

  • Your path is incorrect because you didn't escape \ in it. The fastest way to do it is using @:

    string fil1 = @"C:\Users\mariu\Desktop\Jobboppgave\CaseConsoleApp\Prisfile.txt";
    

    Rebuild your project and problem will be resolved.