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

Upvotes: 0

Views: 3357

Answers (2)

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

AssemblyName.CodeBase property returns the location of the assembly as a URL and as the assembly is a local file the string begins with file:\

Just need to remove this part from uriPath and should work as expected:

string uriPath = GetExecutionFolder() + "\\AppLogMsgs.txt";
uriPath = uriPath.Replace("file:\\", string.Empty);
_fileStream = File.OpenWrite(uriPath); 

Upvotes: 2

Steve
Steve

Reputation: 216293

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

Upvotes: 4

Related Questions