0

Tell me, in the program it is necessary to use the password of one of the users to start the program, but when I use the value of the variable with the password, the following error occurs: cannot convert from string to System.Secure.Security.SecureString; the value must be obtained from the variable, and not entered from the keyboard.




using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;

public class Example
{
    public static void Main()
    {
        string password = "password";

        try
        {
            Process.Start("Notepad.exe", "MyUser", password, "MYDOMAIN");
        }
        catch (Win32Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }
}
COBAuKOT
  • 1
  • 1
  • why don't you search "c# convert string to securestring"? And why do you start notepad with a password? – phuclv Jan 27 '23 at 04:36
  • https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=net-7.0#system-diagnostics-process-start(system-string-system-string-system-string-system-security-securestring-system-string) – Chetan Jan 27 '23 at 04:37
  • You are passing incorrect argument... look at the documentation of `Process.Start`. The 4th argument to this method is `System.Security.SecureString password` but you are passing `MYDOMAIN` instead. that's why you are getting this error. – Chetan Jan 27 '23 at 04:38
  • @Chetan In 1st, 2nd and 4th arguments, ill write values from variables. The values ​​that I wrote in the code example are conditional – COBAuKOT Jan 27 '23 at 04:53
  • how that makes a difference? the 4th argument need to be of type SecureString and you are passing a string in the code. the arguments need to be passed with correct type. You need to read the documentation of the method from the link I shared and also go thru the suggested questions as duplicates to solve your problem. – Chetan Jan 27 '23 at 05:00
  • @Chetan, can u help me? string domain = @".\"; string uname = "Administrator"; ProcessStartInfo proc = new ProcessStartInfo() { UseShellExecute = true, WorkingDirectory = @"C:\Windows\System32", FileName = @"C:\Windows\System32\cmd.exe", Arguments = "/c " + cmd, WindowStyle = ProcessWindowStyle.Hidden }; Process.Start(proc, uname, secure, domain); – COBAuKOT Jan 27 '23 at 05:00
  • you need to edit the original question and add more details/code there... code shared via comments is not readable and does not make the original question any better. – Chetan Jan 27 '23 at 05:01
  • You can set `UserName` and `Password` properties of `proc` object and just start the process by doing `Process.Start(proc)`. – Chetan Jan 27 '23 at 05:08
  • @Chetan, thanks for ask on my question, it's working, u solved my problem – COBAuKOT Feb 02 '23 at 23:13

0 Answers0