Home > Software engineering >  c# can't access file with relative path
c# can't access file with relative path

Time:09-19

I have a text file where I write some errors. If the string file path is written in absolute method, string filePath = @"E:\server\publish\Exceptions.txt"; it works as expected. But if I use a relative path, the file isn't written. The way I use relative path: string filePath = @"../../Exceptions.txt";. What I'm doing wrong?

CodePudding user response:

You can try to use GetFullPath as in similar question

 string sCurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;            
 string sFile = System.IO.Path.Combine(sCurrentDirectory, @"../../Exceptions.txt");
 string sFilePath = Path.GetFullPath(sFile);
  • Related