C # How to get the relative path to a file located in a folder with a large parent?

I am using Visual Studio 2005 (.NET version> 2.0+) to create a Windows application.

According to my observation, the relative path to the file can be specified in relation to the EXE file created in the DEBUG folder. For example: if I specify the path as "images \ image.png", the file inside the "images" folder will be loaded (which is located in the DEBUG folder).

My question is: what if the file exists in the parent folders? Suppose .. "DEBUG" is located in the "BIN" folder and has another "IMAGES" folder, which I must specify in my code.
(bin/images/image.png)

this problem can be avoided by copying the "IMAGES" folder inside the debug folder itself or by specifying an absolute path, but .. I want to know if there is a way to specify the parent folders in the relative path

+3
source share
5 answers

does not work ".."? how in ../../image.png?

edit to add some details: according to MSDN , "A path is also considered relative if it contains" double points ", that is, two periods together in the same path component. This special qualifier is used to designate the directory above the current directory, otherwise called the" parent directory "

" " , .

+5

.. . , "" "images" ..\images.

+4

, , :

DirectoryInfo dir = new DirectoryInfo("../..");
String name = dir.FullName;

.. , .

+3

, CLR ( ) ? .net

+1

, ,

  • bin
    | → Debugging | → Release
  • Images
  • etc.

Once this is available, you can use relative paths from Debug or Release, such as .. /../images/file, etc.

+1
source

Source: https://habr.com/ru/post/1781661/


All Articles