开发者

How Can I Remove The 'file:\\' From the Return Value of Path.GetDirectoryName() in C#

开发者 https://www.devze.com 2023-01-01 12:17 出处:网络
string path = Path.GetDirectoryName( 开发者_如何学CAssembly.GetAssembly(typeof(MyClass)).CodeBase);
string path = Path.GetDirectoryName(
               开发者_如何学C      Assembly.GetAssembly(typeof(MyClass)).CodeBase);

output:

file:\d:\learning\cs\test\test.xml

What's the best way to return only d:\learning\cs\test\test.xml

file:\\ will throw exception when I call doc.Save(returnPath) ,however doc.Load(returnPath); works well. Thank you.


string path = new Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase).LocalPath;


If you want the directory of the assembly of that class, you could use the Assembly.Location property:

string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).Location);

This isn't exactly the same as the CodeBase property, though. The Location is the "path or UNC location of the loaded file that contains the manifest" whereas the CodeBase is the " location of the assembly as specified originally, for example, in an AssemblyName object".


  System.Uri uri = new System.Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase);
  string path = Path.GetDirectoryName(uri.LocalPath);


My first approach would be like this...

path = path.Replace("file://", "");


use the substring string method to grab the file name after file:\

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号