Get not-URI location
When I do
string s = Path.Combine(Folders.Data, fileName);
I've got file:\\...
(the URI location). How can I get c:\...
(the not-URI location)?
EDIT:
The code for the Folders class i开发者_运维技巧s:
public static class Folders
{
public static string App
{
get
{
return Path.GetDirectoryName(
Assembly.GetAssembly(typeof(Folders)).CodeBase
);
}
}
public static string Data
{
get
{
return Path.Combine(App, "Data");
}
}
}
Path.Combine adds the last component, you need to remove file:\ from Folders.Data
You could do something like this
string s = Path.Combine(Folders.Data.Replace("file:\\",""), fileName);
For clear solution try to use Assembly.Location, as shown here it is what you want.
So instead of using:
return Path.GetDirectoryName(
Assembly.GetAssembly(typeof(Folders)).CodeBase
);
Use:
return Path.GetDirectoryName(
Assembly.GetAssembly(typeof(Folders)).Location
);
If you have a file://
URI, you can convert it to a normal path like this:
var fileUri = Folders.Data;
var asPath = new Uri(fileUri).LocalPath;
Get the location using the Location
property instead:
var path = Assembly.GetAssembly(typeof(Folders)).Location;
EDIT:
A few notes on the Location
property from MSDN:
Gets the path or UNC location of the loaded file that contains the manifest.
[Returns] The location of the loaded file that contains the manifest. If the loaded file was shadow-copied, the location is that of the file after being shadow-copied. If the assembly is loaded from a byte array, such as when using the Load(Byte[]) method overload, the value returned is an empty string ("").
To get the location before the file has been shadow-copied, use the CodeBase property.
[Can raise] NotSupportedException The current assembly is a dynamic assembly, represented by an AssemblyBuilder object.
精彩评论