Gergő Szabó
Gergő Szabó

Reputation: 53

C# Directory not found

So my filesystem looks like this on absolute path:

C:\Users\myname\source\repos\projectname\projectname\bin\Debug\data\orders

and I want to use relative path like this:

string path = "~/data/orders/" + sdat + ".txt";
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine(lines[0]);
writer.WriteLine(lines[1]);
writer.WriteLine(lines[2]);
}

but it gives the error:

System.IO.DirectoryNotFoundException: 'Nem található a következő elérési út egy része.: „C:\Users\myname\source\repos\projectname\projectname\bin\Debug\~\data\orders\2021.06.26.txt”.'

Nem található a következő elérési út egy része means Path not found

Upvotes: 1

Views: 50

Answers (1)

MrMoeinM
MrMoeinM

Reputation: 2378

Change

string path = "~/data/orders/" + sdat + ".txt";

to

string path = "data\\orders\\" + sdat + ".txt";

"~" is for url in asp.net not for local filesystem.

Upvotes: 1

Related Questions