Resolved: Help needed with small C# code

Hi, I’ve hit a bit of a wall with my programming skills. I’m trying to make a small program in C# (Language in which I never coded before) and can’t figure out how to make it works. We’ve restricted access to users account on 2 computers (windows 7) to a local account, and, now, I’m trying to make a small programm to mount drives for the users to get their documents. The issue is with the line 10, where my cmd command is. I’m not sure how to make it work from there and have been digging for almost a week on my spare time. If someone can enlight me it would be greatly appreciated. Thanks :slight_smile:

Here’s the code:

using System;
using System.Diagnostics;

namespace userchanger_v1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string str1 = "net use G: \\domain\Users\username /user:username password & net use H: \\domain\Documents\ /user:username password";
            Console.WriteLine("Enter your credentials");
            Console.WriteLine("Username");
            string str2 = Console.ReadLine();
            string str3 = str1.Replace("username", str2);
            Console.WriteLine("Password");
            string str4 = Console.ReadLine();
            string strCmdText = str3.Replace("password", str4);
            Process.Start("CMD.exe", strCmdText);
            Console.WriteLine(strCmdText);
            Console.WriteLine("Operation succesfull");
            Console.WriteLine("Press enter to continue");
            Console.ReadLine();
        }
    }
}

you gota modify windows PATH for CMD.exe unless the program resides where cmd.exe is

I take note of that!
Thanks :slight_smile:

The issue remains with the line 10. Is there another way to write it?
Also, adding the path to the CMD created another error. Here’s the output of Visual Studio.

1>------ Début de la génération : Projet : userchanger_v1, Configuration : Debug Any CPU ------
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(10,56,10,58): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(10,69,10,71): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(10,127,10,129): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(10,140,10,142): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(18,30,18,32): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(18,38,18,40): error CS1009: Séquence d’échappement non reconnue
1>C:\Users\jtcaron\source\repos\userchanger_v1\userchanger_v1\Program.cs(18,47,18,49): error CS1009: Séquence d’échappement non reconnue
========== Génération : 0 completed, 1 failed, 0 updated, 0 ignored ==========

Please use grave ticks.

```
code goes here
```


    using System;
    using System.Diagnostics;

    namespace userchanger_v1 {
       class Program {
          static void Main(string[] args) {
             const string str1 = “net use G: \domain\Users\username /user:username password & net use H: \domain\Documents\ /user:username password”;
             Console.WriteLine(“Enter your credentials”);
             Console.WriteLine(“Username”);
             string str2 = Console.ReadLine();
             string str3 = str1.Replace(“username”, str2);
             Console.WriteLine(“Password”);
             string str4 = Console.ReadLine();
             string strCmdText = str3.Replace(“password”, str4);
             Process.Start(“CMD.exe”, strCmdText);
             Console.WriteLine(strCmdText);
             Console.WriteLine(“Operation succesfull”);
             Console.WriteLine(“Press enter to continue”);
             Console.ReadLine();
          }
       }
    }

2 Likes

Edited. Thanks for the trick.

1 Like

After some tests, the problem seams to be the \ in my strings. Not sure how to aleviate that problem tho.

If you need to use backslash in a string, just put 2 of them. Backslash is used as an escape character in C# (and many other languages also).

2 Likes

I think there’s another issue here in that you need to tell CMD that you’re passing it a string. You can do this either by using /C or /K in front of the command. /C will terminate CMD after the command is executed, /K will leave CMD running.

For example:

const string str1 = "/C" + @"net use G: \domain\Users\username /user:username password & net use H: \domain\Documents\ /user:username password";
1 Like

Would you Mind to explain what the @ means? It works really well tho and could use /K to test it out, thank you so much! :slight_smile:

I’ve only programmed in actionscript (not that it’s usefull anymore), html and css in the past, and, that was in high school, 7 years ago. I corrected and modified some code since. Thank you very much for the info, it works now :slight_smile:

@JTip you’re welcome :slight_smile:

I think this answer on Stack Overflow explains @ prefix best:

2 Likes

nice, saves you some typing