none
Using administrator user to execute a program from C# RRS feed

  • Question

  • I've got a problem executing a c# code which executes other c# program, it has to open with an administrator user. It works fine in windows 8 but when i want to execute it in windows 10 it says that the user or password is wrong. Maybe, the way you log in  windows 8 is  differently with win 10. In this following code you will have to change it to open a document or image(as you wish) the thing is that you have to open it with cmd and the administrator user.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.DirectoryServices;
    using System.Diagnostics;
    using System.IO;
    using System.Security;
    namespace Ejecutable
    {
        static class Program
        {
            /// <summary>
            /// Punto de entrada principal para la aplicación.
            /// </summary>
            [STAThread]
            static void Main()
            {
                try
                {
                    
                     string cmdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
                    string workingDirectory = @"C:\Users\Desktop";
                    string pathToFile = Path.Combine(workingDirectory, "distri2017.pdf");
                    string arguments = string.Format("/c start {0}", pathToFile);
                    var password = new SecureString();
                    foreach (char c in "password")
                        password.AppendChar(c);
                    var processStartInfo = new ProcessStartInfo()
                    {
                        FileName = cmdPath,
                        Arguments = arguments,
                        WorkingDirectory = workingDirectory,
                        UserName = "ADMINISTRATOR",
                        Domain = Environment.MachineName, // Could use domain
                       Password = password,
                        UseShellExecute = false,
                    };
                    Process.Start(processStartInfo);
                   
                 
                }

                catch (Exception e)
                {
                    MessageBox.Show("COULDNT OPEN: "+e);
                }

            }
        }
        }

    Friday, December 1, 2017 8:59 PM

All replies

  •  

    Maybe, the way you log in  windows 8 is  differently with win 10.

    No it is not different. If the credentials being given are wrong, then they are wrong.

    • Marked as answer by JoseHHS Friday, December 1, 2017 9:23 PM
    • Unmarked as answer by JoseHHS Friday, December 1, 2017 9:23 PM
    Friday, December 1, 2017 9:02 PM
  • But how does it work fine in win 8 and not in win 10?
    Friday, December 1, 2017 9:23 PM
  • Have you verified that the Administrator account in Windows 10 is enabled?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, December 1, 2017 9:27 PM
  • I suggest that you make another admin account on Win 10 and test it.
    Friday, December 1, 2017 9:41 PM
  • Yes, the account is correct in win 10
    Monday, December 4, 2017 7:15 PM
  • Yes, the account is correct in win 10

    So if you make a new admin account and it works, then what?
    Monday, December 4, 2017 10:39 PM
  • Hi JoseHHS,

    Do you use a different administrator user? does it work for you?

    If the issue still exist, please feel free let us know.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 4:48 AM
    Moderator