.NET settings relative path

I am working on an application where I have a folder of images relative to my application root. I want to be able to specify this relative path in the constructor "Properties ->" for example. "\ Images \". The problem I am facing is in cases where the Environment.CurrentDirectory is changed with the OpenFileDialog, the relative path is not resolved in the right place. Is there a way to specify a path in the settings file that means it always starts from the application directory and not from the current directory? I know I can always dynamically concatenate the application path to the start of the relative path, but I would like my settings property to be able to resolve itself.

0


source to share


6 answers


As far as I know, there are no built-in functions that will allow this type of resolution. Your best option is to dynamically define applications executing the directory and concatenate the image path with it. You don't want to use it Environment.CurrentDirectory

specifically for the reasons you talk about - the current directory may not always be the correct one for this situation.

The safest code I've found to find the runtime build location is:



public string ExecutingAssemblyPath()
{
   Assembly actualAssembly = Assembly.GetEntryAssembly();
   if (this.actualAssembly == null)
   {
      actualAssembly = Assembly.GetCallingAssembly();
   }
   return actualAssembly.Location;
}

      

+1


source


Are you looking for Application.ExecutablePath? This should tell you where the application executable is, remove the executable name and then add your path to it.



+1


source


2 options:

  • Code that uses this parameter can allow customization relative to the directory of the current executing assembly.
  • You can create your own type that serializes as a string relative to the executing assembly and has a full path accessory that will resolve against the directory of the current executing assembly.

Sample code:

string absolutePath = Settings.Default.ImagePath;
if(!Path.IsPathRooted(absolutePath))
{
    string root = Assembly.GetEntryAssembly().Location;
    root = Path.GetDirectoryName(root);
    absolutePath = Path.Combine(root, absolutePath);
}

      

The best part about this code is that it allows you to use the full path or relative path in your settings. If you need the path to be relative to a different assembly, you can change which location you are using - GetExecutingAssembly()

will provide you with the assembly location with the code you are working in and it GetCallingAssembly()

will be good if you go with option 2.

0


source


This seems to work in both WinForms and ASP.NET (gives the path to the config file ):

new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).Directory;

      

For Windows and Console apps, the obvious way is to use:

Application.StartupPath

      

0


source


I suggest you use Assembly.CodeBase like below:

public static string RealAssemblyFilePath()
{
   string dllPath=Assembly.GetExecutingAssembly().CodeBase.Substring(8);
   return dllPath;
}

      

You can try Application.ExecutablePath . But you need to make a reference to System.Windows.Forms. This might not be a good idea if you want your class library to avoid shapes and UI materials.

You can try Assembly.GetExecutingAssembly (). Location . But if you somehow execute a "Shadow Copy" before starting your application (for example, the default NUnit), this property will return you the location of the shadow copy, not the actual physical location.

The best way is to implement a function that calls the Assembly object's CodeBase property and chops off the unneeded portion of the string.

0


source


For this I use the following two methods:

public static IEnumerable<DirectoryInfo> ParentDirs(this DirectoryInfo dir) {
    while (dir != null) {
        yield return dir;
        dir = dir.Parent;
    }
}
public static DirectoryInfo FindDataDir(string relpath, Assembly assembly) {
    return new FileInfo((assembly).Location)
        .Directory.ParentDirs()
        .Select(dir => Path.Combine(dir.FullName + @"\", relpath))
        .Where(Directory.Exists)
        .Select(path => new DirectoryInfo(path))
        .FirstOrDefault();
}

      

The reason parent directories are easier to use during development is when scripts with various scripts end up being stored in directories like bin\x64\Release\NonsensePath\

.

0


source