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

Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • possible duplicate of [Is there a .NET Framework method for converting file URIs to paths with drive letters?](http://stackoverflow.com/questions/278761/is-there-a-net-framework-method-for-converting-file-uris-to-paths-with-drive-le) – nawfal Jan 10 '14 at 20:04

5 Answers5

61
string path = new Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase).LocalPath;
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 6
    I'd like to add that, while Chris' solution was marked as Answer, it was Matthew's solution that actually provided a proper solution to the question in the title and to my problem: How to convert a file:// style path to a C:\ style path. Thanks Matthew ! – Vincent Vancalbergh Apr 26 '12 at 09:53
  • The problem with this approach is that if you have any invalid URI character (for example, the sharp one #) this will be omitted by new Uri() function and your path will be wrong. You will have to use something like WebUtility.UrlEncode in order to correctly get the right path. – Fabiano Nov 08 '19 at 20:40
  • @Fabiano I have a path with #, using UrlEncode throws an exception when running new Uri(WebUtility.UrlEncode(myWeirdPath)); Alternatives? – Noman_1 Oct 29 '20 at 22:46
15

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

Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
  • Thanks for your Simple but perfect solution. @all, Thank you. – Nano HE Jun 01 '10 at 06:00
  • 5
    Location is the assembly location after shadow copying, while CodeBase is the original location, before the shadow copying happened. So if shadow copying is enabled, and you want the original location, one must use CodeBase. – Marius Bancila Nov 15 '12 at 10:18
9
  System.Uri uri = new System.Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase);
  string path = Path.GetDirectoryName(uri.LocalPath);
Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
  • I noticed that the spaces in the path were replaced with "%20" if I used System.Uri – Alex Mar 20 '19 at 21:07
  • @AlexKoshulyan - you are right for some properties of Uri, like AbsolutePath. But Uri.LocalPath does not seem to replace spaces with %20 - at least from what I can see. – Moe Sisko Mar 25 '19 at 02:30
4

My first approach would be like this...

path = path.Replace("file://", "");
Community
  • 1
  • 1
The King
  • 4,600
  • 3
  • 39
  • 58
  • 8
    While this might answer the question in hand for OP, this is a poor solution. The uri class handles a lot more corner cases. – nawfal Jan 10 '14 at 20:46
  • 2
    @nawfal - "The uri class handles a lot more corner cases"...and it also introduces plenty of corner cases of its own. For example, if the CodeBase for your assembly is in file:///c:/temp/#my_test#/WindowsFormsApplication1/WindowsFormsApplication1/bin/Debug/WindowsFormsApplication1.EXE and you pass that CodeBase value to the Uri constructor, you get c:\\temp for uri.LocalPath. The hash characters cause it not to work as one would hope it would. – dcp May 04 '16 at 14:19
  • Another problem is when running on linux. There the result would be "file:/home/something" where you only need to remove "file:" – nivs1978 Jan 17 '23 at 10:12
0

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

VoodooChild
  • 9,776
  • 8
  • 66
  • 99