[C#] - Bypass command line arguments working directory redirection

Discussion in 'Mixed Languages' started by Josh Cell, Feb 21, 2012.

  1. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,170
    120
    #1 Josh Cell, Feb 21, 2012
    Last edited by a moderator: Apr 20, 2017
    If we pass an command for our applications, the "Working Directory" of this application, will be changed by command passer (in this case, the cmd.exe).

    With this declarations, you will force the work of directories independently of the command line:

    PS:

    I have an application on C:\ called MyApp.exe;

    It write an text file by:

    Code:
    File.WriteAllText("MyText.txt", "Hello.");
    If the MyApp.exe receive any command line argument, the MyText.txt will be saved in the working directory of cmd.exe, not will save in the root.

    This code will get the root for you:

    Code:
            public static string CurrentPathOfApp = Application.ExecutablePath.Replace(Path.GetFileName(Application.ExecutablePath), "");
            public static string CurrentPathOfExe = Application.ExecutablePath;
    CurrentPathOfApp contains the root directory string;

    CurrentPathOfExe contains the root directory + executable path.

    And, finally, it solve the problem, the application now can receive new command arguments, and perfectly will work in root directory without any redirection*

    *PS: You need add this code before any save string code:

    Code:
    File.WriteAllText(CurrentPathOfApp + "\\MyText.txt", "Hello.");
    Yeah, it simply, good day ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #2 Calistoga, Feb 23, 2012
    Last edited by a moderator: Apr 20, 2017
    Glad you posted this Josh, there's probably many with this problem. I have some other solutions here as well :)

    You can change the current working directory if you want:
    Code:
    using System.IO;
    
    Directory.SetCurrentDirectory(CurrentPathOfApp);
    
    See MSDN.

    A better solution however, might be writing the file somewhere you know for certain that you will have write access. A good choice could be the ApplicationData (per user)
    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    or the Common ApplicationData (for all users) folder.
    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    See MSDN.

    Personally I usually create a subfolder in either one of the directories above with a GUID as the name. This is to be ~100% sure that no other application will mess with my data unknowingly.

    Visual Studio includes a GUID generator, but for those who uses something else: Online GUID Generator.
    Code:
    For example:
    %AppData%\MyAppName\606c6669-e9fa-4c0f-a0b5-d021f5a31112\...
    
    Eventually one could look into the IsolatedStorage API which takes care of everything :)
     
  3. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,170
    120
    Thanks Calistoga for all info and code examples :)

    In some time, I was problems with arguments in forms.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...