Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# code puts in a backslash

Options
  • 17-12-2010 11:21am
    #1
    Registered Users Posts: 4,037 ✭✭✭


    This is related to my earlier question. I have successfully created a batch file to add a scheduled task but in order to add it I need to disable UAC temporaily on the PC I am adding the task to.
    I have a script to do it:
    C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
    
    
    However I cannot call this in C# using System.Diagnostics.Process.Start because of the backslashes in the string. It gives me an error ("The system cannot find the file specified").
    This is because when I look in the immediate window at what the argument I am passing in to it contains (a Stringbuilder variable called "builder") there is an extra backslash everywhere there is supposed to be only one. It doesn't do it with forward slashes and if I put up a message box of the string builder (as I have done below) then it shows the correct path.
    (See my code below)
    string command1= @"C:\Windows";
               string command2 = @"\System32";
               string command3 =@"\cmd.exe";
               string command4=@"/k %windir%";
               string command5=@"\System32";
               string command6= @"\reg.exe";
               string command7="ADD HKLM";
               string command8=@"\SOFTWARE";
               string command9=@"\Microsoft";  
               string command10=@"\CurrentVersion";
               string command11=@"\Policies";
               string command12=@"\System";
               string command13 = "/v EnableLUA /t REG_DWORD /d 0 /f";
    
               StringBuilder builder = new StringBuilder();
               string[] data = { command1, command2, command3, command4, command5, command6, command7, command8, command9, command10, command11, command12, command13 };
              
              
                   builder.Append(data[0]);
                   builder.Append(data[1]);
                   builder.Append(data[2]);
                   builder.Append(data[3]);
                   builder.Append(data[4]);
                   builder.Append(data[5]);
                   builder.Append(data[6]);
                   builder.Append(data[7]);
                   builder.Append(data[8]);
                   builder.Append(data[9]);
                   builder.Append(data[10]);
                   builder.Append(data[11]);
                   builder.Append(data[12]);
                   
    
                       
            
             builder.Replace("\\",@"\");
    
    
             System.Windows.Forms.MessageBox.Show(builder.ToString());
    
                System.Diagnostics.Process.Start(builder.ToString());
    


Comments

  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    The backslashes are fine, don't worry about them.
    The path you are creating isn't a file, it's a file and arguments.
    System.Diagnostics.Process.Start(file,arguments)
    Split the file and the arguments and pass both of them in.
    Also, make sure you have a space after reg.exe
    and you don't need this
    builder.Replace("\\",@\);


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Cheers, that worked when I did it like you said
    System.Diagnostics.Process.Start("C:\\Windows\\System32\\cmd.exe","/k %windir%\\System32\\reg.exe ADD HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System /v EnableLUA /t REG_DWORD /d 0 /f");
    
    but the dos window stays open after. I also need to start a Windows Service immediately after disabling UAC (that's why I need to disable it; I can't start it while it is enabled).
    I want to start it using "net start" from a Dos Window but it won't do it if I try it the same was as above
     System.Diagnostics.Process.Start("C:\\Windows\\System32\\cmd.exe", "net start EnergywatchITService");
    


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    net is an exe in System32 I think.
    So you can do
    "C:\\Windows\\System.32\net.exe"
    and pass the
    "start EnergywatchITService" part.

    Maybe you don't need to start cmd.exe in either case?

    Also, any reason you are creating strings, then an array, and then a stringbuilder?


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Giblet wrote: »
    net is an exe in System32 I think.
    So you can do
    "C:\\Windows\\System.32\net.exe"
    and pass the
    "start EnergywatchITService" part.

    Maybe you don't need to start cmd.exe in either case?

    Also, any reason you are creating strings, then an array, and then a stringbuilder?

    I had a problem with teh backslashes, thought I could edit them out with a string builder.


Advertisement