How to get the path of the folder of the dll?

Elad770

Member
Joined
Oct 4, 2021
Messages
20
Programming Experience
1-3
Hello
I have a problem and I have no idea why it is happening.
I am trying to run a class from a dll file, one of whose functions reads from a text file in the debug folder by the StreamReader class.
The problem is that when I run the additional project that calls the class from the first project and the function in question is run, I get the path of the second project and not of the dll? what is the mistake.
Here is the code:
C#:
namespace LibraryDiagnosis
{
public class Diagnostic
{
   Dictionary<string, double[][]> ranges;
   public Diagnostic()
   {
     CreateAllRanges();
   } 
  
   private void CreateAllRanges()
   {
     //Create all standard test ranges
      ranges = new Dictionary<string, double[][]>();
      string line;
       //error path file
       using (StreamReader file = new StreamReader("metrics.txt"))
       {
          while ((line = file.ReadLine()) != null)
          {
              string[] delimiters = line.Split(new char[] { ',' });
              double[][] dou = new double[int.Parse(delimiters[1].ToString())][];
              for (int i = 2,j=0; i < delimiters.Length; i+=2,j++)
              {
                  dou[j] = new double[2];
                  dou[j][0] = double.Parse(delimiters[i].ToString());
                  dou[j][1] = double.Parse(delimiters[i+1].ToString());
              }
              ranges.Add(delimiters[0].ToString(), dou);
           }
           file.Close();
       }
   }
}

//project two console
using LibraryDiagnosis;
Diagnostic dia = new Diagnostic();
 
If you provide only a file name and no path then it is assumed to be in the current directory of the current process. All relative paths are assumed relative to that current directory.

I'm on a phone right now so I'm not able to get all the specifics but you should be able to use Assembly.GetExecutingAssembly or the like to get an Assembly object and then get the file system location of that.

To be honest though, that seems a bit dodgy to me. I'd probably be more inclined to specify to users of your library that it and the data file must be in the same folder as the EXE and use Application.StartupPath, or else specify that the file path must be included in the application config file.
 
As an aside, there is no need to call ToString() on lines 22, 26, 27, and 29. You are accessing an array of strings, therefore the elements of the array are already strings.
 
As an aside, there is no need to call ToString() on lines 22, 26, 27, and 29. You are accessing an array of strings, therefore the elements of the array are already strings.
Yes, I know that it is itself a string, I still have to perform ToString because I get an error without it, and it happened to me many times with the Parse function, basically I would prefer to use the static class Convert
 
If you provide only a file name and no path then it is assumed to be in the current directory of the current process. All relative paths are assumed relative to that current directory.

I'm on a phone right now so I'm not able to get all the specifics but you should be able to use Assembly.GetExecutingAssembly or the like to get an Assembly object and then get the file system location of that.

To be honest though, that seems a bit dodgy to me. I'd probably be more inclined to specify to users of your library that it and the data file must be in the same folder as the EXE and use Application.StartupPath, or else specify that the file path must be included in the application config file.
I tried to use with this function
Assembly.GetExecutingAssembly
But it gives me the folder of the main process and not the dll.
I didn't understand exactly how I still make it run the dll that will point to its relative folder.
 
Yes, I know that it is itself a string, I still have to perform ToString because I get an error without it, and it happened to me many times with the Parse function, basically I would prefer to use the static class Convert
What error?
 
I tried to use with this function
Assembly.GetExecutingAssembly
But it gives me the folder of the main process and not the dll.
I didn't understand exactly how I still make it run the dll that will point to its relative folder.
What he meant is that Assembly.GetExecutingAssembly() needs to be inside that DLL, not inside your main process. The DLL will have to figure out were it lives, and then find the "metrics.txt" file in the same directory that it lives in.
 
What error?
What he meant is that Assembly.GetExecutingAssembly() needs to be inside that DLL, not inside your main process. The DLL will have to figure out were it lives, and then find the "metrics.txt" file in the same directory that it lives in.
The truth is that right now I'm working in Visual 2022 and there really isn't an error, but in 2017 quite a few times I had to use Parse as in the example here
I would get a compilation error red bar claiming that it didn't recognize the object even though it was a string.
 
More often than not, people run into that issue when they declared the variable as object, and then assigned a string to it. So yes, you know it is a string, but the compiler does not, and so it is giving you an error regarding the Parse() method expecting a string, not an object.

On the flip side, most of the Convert methods will take an object. There is no need to call ToString().
 

Latest posts

Back
Top Bottom