C # Get relative path in reference assembly

I have 2 projects Project A, Project B, project A has a link to project B, project A is executable.

Project A --> Project B 

inside project B there is a directory called "MyFolder"

therefore, the soulotion hierarchy is as follows:

  MySolution - A - B - MyFolder 

how do I get the relative path to MyFolder using in project A (Executable).

I found an answer that states the following:

  sring path = Assembly.GetAssembly(typeof(SomeClassInBProject)).Location; 

The path I returned to is the path to B.dll in bin \ debug, how can I get the path in this dll.

Edit:

iv'e also tried:

  Assembly assembly = Assembly.GetAssembly(typeof(SomeClassInBProject)); FileStream fs = assembly.GetFile(@"MyFolder\myFile"); and FileStream fs = assembly.GetFile("MyFolder\myFile"); and FileStream fs = assembly.GetFile("myFile"); 

fs i am always null.

+6
source share
2 answers

Is Uri.MakeRelativeUri what you are looking for?

 string pathA = Assembly.GetExecutingAssembly().Location; string pathB = Assembly.GetAssembly(typeof(SomeClassInBProject)).Location; Uri pathAUri = new Uri(pathA); Uri pathBUri = new Uri(pathB); string relativePath = pathAUri.MakeRelativeUri(pathBUri).OriginalString; string relativeMyFolder = Path.Combine(relativePath, "MyFolder"); 

Update

You can use the Assembly.GetFile () method, which returns a FileStream. FileStream has a Name property that you could use in the above code.

+2
source

If there is no reason why you cannot do this, I recommend that you open the * properties window and set the Build Action property to Embedded Resource and make sure that the Copy to Output Directory parameter is set to Do not copy . Then you can use Assembly.GetFile () to access it.

 Assembly assembly = Assembly.GetAssembly(typeof(SomeClassInBProject)); using (FileStream fs = assembly.GetFile("myfile")) { // Manipulate the FileStream here } 

* With the selected file, press Alt + Enter or right-click the file and select Properties

0
source

All Articles