Search code examples
c#reflectionurifilestream

Why am I getting, "URI formats are not supported" with this code?


I have this code that should assign a string path value to the "uripath" string variable:

private readonly FileStream _fileStream;

. . .

string uriPath = GetExecutionFolder() + "\\AppLogMsgs.txt";
_fileStream = File.OpenWrite(uriPath); 

. . .

private string GetExecutionFolder()
{
    return Path.GetDirectoryName       
(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

...but it crashes when trying to execute the "_fileStream = File.OpenWrite(uriPath);" line.

Why, and what do I need to do to rectify this revoltin' development?

The value of "uriPath" when it crashes is:

file:\C:\Projects\ReportScheduler\ReportScheduler\bin\Debug\AppLogMsgs.txt


Solution

  • If your intent is to know the location of the loaded file that contains the manifest then you could change your method to

    private string GetExecutionFolder()
    {
        return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    }
    

    See Location Property