0

I have created two projects under the same solution. ProjectA is a Windows Form Application and ProjectB is a simple console application.ProjectB will be executed from ProjectA with admin privileges.
Sample from ProjectA

private void btnFinish_Click(object sender, EventArgs e)
        {
            ipAddress = txtIP.Text;
            bindingPort = txtPort.Text;
            if (!fileChosen)
            {
                CreateCertificate();
                //
            }
            //After this step i want to execute ProjectB with admin provileges with 3 parameters
            ExecuteB_AsAdminWithPrivileges(ipAddress, bindingPort, serverCert);
        }
    }

So when i click the button name Finish i want the ProjectB.exe to be executed with parameters that i will give from ProjectA.
And ProjectB will look sth like:

public static void StoreAndBindCertificate(string pfxFileServerCert, string ipAddress, string ipPort)
        {
//
}

This is the method which will be using the parameters from ProjectA.
How can i get the Parameters from ProjectA to this method in ProjectB?

drgmak
  • 1,135
  • 10
  • 13

2 Answers2

1

Update

ProgramA{
string ip ="123.123.123";
File.WriteAllText("c://MtDataFromA.txt","ip="+ip);
}


private void btnFinish_Click(object sender, EventArgs e)
            {
                ipAddress = File.WriteAllText("c://MtDataFromA.txt");//some algorithem to find the ip from text file

    }


public static void StoreAndBindCertificate(string pfxFileServerCert, string ipAddress, string ipPort){


        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "YourFile.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "ipAddress"+" " +"ipPort";

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
catch
        {
             // Log error.
        }
}

link

Community
  • 1
  • 1
The scion
  • 1,001
  • 9
  • 19
  • In this case means that i already have the ipAddress and ipPort parameters here. My question is how can i get these two parameters from the other project and use them here? – drgmak Jun 02 '16 at 13:03
  • This method StoreAndBindCertificate belongs to ProjectB namespace. So, i want to execute this ProjectB.exe as admin from ProjectA – drgmak Jun 02 '16 at 13:04
  • Ok, You can store your data like ipPort and ipAdress in some text file on some folder when ProjectA is running and then just read it from the file that you created inside this function. – The scion Jun 02 '16 at 14:21
  • Thanx for the help – drgmak Jun 02 '16 at 14:28
1

You could use this method:

public static int RunProcessAsAdmin(string exeName, string parameters)
{
    try {
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.UseShellExecute = true;
        startInfo.WorkingDirectory = CurrentDirectory;
        startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
        startInfo.Verb = "runas";
        //MLHIDE
        startInfo.Arguments = parameters;
        startInfo.ErrorDialog = true;

        Process process = System.Diagnostics.Process.Start(startInfo);
        process.WaitForExit();
        return process.ExitCode;
    } catch (Win32Exception ex) {
        WriteLog(ex);
        switch (ex.NativeErrorCode) {
            case 1223:
                return ex.NativeErrorCode;
            default:
                return ErrorReturnInteger;
        }

    } catch (Exception ex) {
        WriteLog(ex);
        return ErrorReturnInteger;
    }
}

The first parameter will be your .exe file and the second one will be the parameters you want to give to your .exe file After this you should make changes in your .exe file in the main section. Something like:

static void Main(string[] args)
        {
            if (args.Length <= 1) return;

            try
            {
                if (args.Length == 2)
                {
                    _IpAddress = args[0];
                    _IpPort = args[1];
                    FunctionName(_IpAddress, _IpPort);
                }
                else
                {
                    _return
                }
            }
            catch (Exception)
            {
                throw new Exception("Invalid number of parameters!");
            }
        }

I hope this helps.

etrupja
  • 2,710
  • 6
  • 22
  • 37