1

Essentially I am trying to create a C# program that goes into a local directory and performs some tasks by executing a batch file. The batch file itself is located in the AppData Roaming table and requires the C# program to know the username of the computer, whatever it may be.

This is what I currently have:

static void Main()
{
    ProcessStartInfo processInfo = new ProcessStartInfo("C:\\Users\\%username%\\AppData\\Roaming");
    processInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = true;
    Process batchProcess = new Process();
    batchProcess.StartInfo = processInfo;
    batchProcess.Start();
}

Notice how I've added %username% but it doesn't seem to understand environmental variables.

What can I add to my code?

My code is not exactly a duplicate as the use case is entirely different, I am referencing the app data roaming directory and opening a batch located in it.

  • You wanna get username of your computer? – Mikev Apr 08 '19 at 13:19
  • Yes, I would for the program to understand what the current logged in username is –  Apr 08 '19 at 13:21
  • solved here: https://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c – andreasnico Apr 08 '19 at 13:22
  • https://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c – Mikev Apr 08 '19 at 13:22
  • The duplicate answers the question, but given the code example there is a better approach to use. I suggest to reopen here – Steve Apr 08 '19 at 13:25
  • 1
    You probably want to use `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)` instead of building that path yourself. – huysentruitw Apr 08 '19 at 13:27
  • @Steve I just submitted a reason why my code is different, I would appreciate your take on the matter –  Apr 08 '19 at 13:29
  • The comment above from @huysentruitw is all what you need to do. Get the result of that line in a string variable and use it instead of your hard coded path. – Steve Apr 08 '19 at 13:31
  • By the way, passing just the directory name to ProcessStartInfo constructor will not execute your batch. You seems to missing something there or in the code below before starting the process. – Steve Apr 08 '19 at 13:34
  • To combine the roaming path with your executable name, use `Path.Combine()` – huysentruitw Apr 08 '19 at 13:35
  • 1
    The way the title is worded, the linked duplicate makes sense, but from what is intended, I would think something like the following would be a more appropriate duplicate? https://stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata – Broots Waymb Apr 08 '19 at 13:36

1 Answers1

0

You can get the user folder for the current user like this:

string userfolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

And so the path you want would be like:

string path = Path.Combine(userfolder, "AppData\\Roaming");
ProcessStartInfo processInfo = new ProcessStartInfo(path);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • How can I implement this into my path? –  Apr 08 '19 at 13:25
  • @Winston I edited my answer please have a look – Ashkan Mobayen Khiabani Apr 08 '19 at 13:31
  • You know, we can change the roaming folder. This will fail if the roaming folder is no more inside the user folder or if we have a different name. This happens a lot in more sophisticated environments than an home pc. – Steve Apr 08 '19 at 13:36
  • How can I use the "path" string, instead of the path given in new ProcessStartInfo? –  Apr 08 '19 at 13:39
  • I added it to the answer. – Ashkan Mobayen Khiabani Apr 08 '19 at 13:42
  • @Steve No, I had no idea, what can be done about this? I need to make sure the appdata roaming folder is always found –  Apr 08 '19 at 13:46
  • That's the reason to use the appropriate and dedicated _Environment.SpecialFolder.ApplicationData_ instead of building manually what you think should be the roaming folder for the current user. We have told you this 20 minutes ago – Steve Apr 08 '19 at 13:50
  • My apologies, this is what I ended up using: –  Apr 08 '19 at 14:08
  • 1
    string userfolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string path = Path.Combine(userfolder, "test1.bat"); –  Apr 08 '19 at 14:08