Search code examples
c#streamwriter

How can I fix my path? System.IO.DirectoryNotFoundException: Could not find a part of the path


Hey I want to write the data (ruck2) to a .txt file in my local folder. I have tried many paths but none of them worked. The last path I was trying is "H:\Test/". How can I fix my path?

void Ableitung2()
        {

            delZ2 = output[output.Count - 1] - output[output.Count - 2]; // delta z
            delT2 = output_time[output_time.Count - 1] - output_time[output_time.Count - 2]; // delta t
            delT2 = delT2 * 1000; // aus millisekunden sekunden
            ruck2 = delZ2 / delT2; // hier wird die ableitung berechnet --> Ruck


            var path = "H:/Test/";
            var filename = Path.Combine(path, "test.txt");
            using (var writer = new StreamWriter(filename))
            {
                writer.WriteLine(ruck2);
            }

Unhandled Exception:

System.IO.DirectoryNotFoundException: Could not find a part of the path "/H: \Test/test.txt".

UPDATE

Ok I found that the path of var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); is /data/user/0/accelerometer2.accelerometer2/files

When I searched for the folder on my Android I could not find it so I have created a new one and I have no Exceptions anymore!

I wanted to create a txt file manually but it didnt worked so I guess thats why no file will be created.


Solution

  • I am guessing you're trying this on Windows. If that's the case then you are using forward slashes instead of backslashes. You also mentioned an Android device somewhere. You will frequently encounter errors when trying to write a file to an external device.

    Try the following and let me know if it works:

    change

    var filename = Path.Combine(path, "test.txt");
    

    to

    var filename = "C:\Temp\test.txt";