Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

path

string path = Path.GetDirectoryName(                      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.

like image 916
Nano HE Avatar asked Jun 01 '10 05:06

Nano HE


People also ask

How to Remove file path from file name in c#?

Delete() method takes the full path (absolute path including the file name) of the file to be deleted. If file does not exist, no exception is thrown. The following code snippet deletes a file, Authors. txt stored in C:\Temp\Data\ folder.

What is System IO path?

Remarks. A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.


2 Answers

string path = new Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase).LocalPath; 
like image 199
Matthew Flaschen Avatar answered Sep 19 '22 00:09

Matthew Flaschen


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".

like image 22
Chris Schmich Avatar answered Sep 19 '22 00:09

Chris Schmich