I need to create a folder and file inside the Debug directory. How to prescribe a path so as not to prescribe the whole path (F: \ Projects \ Programms), but in order to specify the path from the initial directory of ekzeshnik?

For example, I create an XML file through code. It is saved to the Debag directory, and I need to go to another directory that has already been created in the Debag directory.

  • @demiurge, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

4 answers 4

You should not want to create a file in the directory where the process is running from. On a normal, lively system, your process will be in a write-protected directory inside %ProgramFiles% .

Create files in %APPDATA%\<название вашего приложения> .

  • one
    @Gennadiy Pisarev: On a normal system, the directory in which the program is located is a subdirectory of Program Files . There is no write access, check for yourself. - VladD

Use AppDomain.CurrentDomain.BaseDirectory .

    Application.StartupPath

    For example:

     Application.StartupPath + @"\\Directory\\filename.xml"; 

      According to the recommendations, in the folder with the program there should be no files with settings, cache and other files that are constantly changing. It is better to use the following directories:

      • Environment.GetFolderPath(ApplicationData) - files tied to a user walk around the domain (for example, settings)
      • Environment.GetFolderPath(LocalApplicationData) - files tied to the user and computer, do not walk across the domain (for example, cache)
      • Environment.GetFolderPath(CommonApplicationData) - files common to several applications on the computer, do not walk across the domain (for example, common to several configuration programs)

      To all the folders above, you should add a subpath of the "YourCompany \ YourApplication" type.

      If you insist on using the path to the executable file, you can use Assembly.GetExecutingAssembly().CodeBase .