is it possible to run a C# project Already build using another project? there is any way to invoke the c# execution using a c# Code.
In here I need to execute the Already Compile Project inside a another project.
You can run an exe using Process.Start
ProcessStartInfo ps = new ProcessStartInfo("YourExecutable.exe", "any arguments");
ps.WindowStyle = ProcessWindowStyle.Hidden; // or exclude this line to show it
ps.CreateNoWindow = true; // and this line
ps.UseShellExecute = false;
Process process = new Process();
process.StartInfo = ps;
process.Start();
There are additional ways to get the output of your process for displaying or logging but that is easily searchable.
Related
I am using .Net 4.6.1 for a Winform application.
I neet to execute some command to configure user settings through powershell.
The powershell reference that I used is under C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0, so (I guess) its version is 3.0.
I use following to execute powershell command:
System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create();
shell.AddScript("Get-LocalUser -Verbose");
then check
shell.Streams.Error[0].Exception;
and see following error:
The term 'Get-LocalUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I also tried executing the command through a powershell file like:
var psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Verb = "runas";
psi.FileName = #"powershell.exe";
psi.Arguments = $" -File \"<path-to-file>\\MyScript.ps1\"";
try
{
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
string f = proc.StandardError.ReadToEnd();
proc.WaitForExit();
exitCode = proc.ExitCode;
}
But I got the same error.
How can I call a powershell script or powershell within c#?
EDIT: it turns out to be 32-64 bit issue. When I switched to x64, it worked just fine.
You should ensure you compile your C# code to target x64 (i.e. 64-bit) as most PowerShell modules will only work in 64-bit mode. If your C# code runs in 32-bit mode, it will invoke a 32-bit PowerShell environment, which will cause problems.
I had the similar experience and succeeded only after changing ExecutionPolicy. The error was equal, but all the details seemed fine.
PS 7.1.3, Microsoft.Powershell.SDK.
Use this:
PowerShell.Create().AddCommand("Set-ExecutionPolicy")
.AddParameter("-ExecutionPolicy", "Bypass")
.Invoke();
Run this code once before doing anything with the PowerShell.
I've been trying to run an AutoIt script file (.au3) from my WPF application. It does not throw any error but does not work.
AutoItX.Run("test.au3", #"C:\Folder\",1);
But it works well with below line.
AutoItX.Run("calc.exe","",1);
C# solution I have :
//switch vpn
Console.WriteLine("Refreshing VPN");
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files (x86)\AutoIt3\AutoIt3.exe";
process.StartInfo.Arguments = " \"" + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "RandomConnectProtonVpn.au3") + "\"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();// Waits here for the process to exit.
AutoItX does not have the capability of running AutoIt scripts. To do that, you need to use the AutoIt interpreter, e.g. like:
AutoItX.Run(#"""C:\Program Files (x86)\AutoIt3\AutoIt3.exe"" /AutoIt3ExecuteScript test.au3", #"C:\Folder\", 1);
Also see my answer here: how can I call AutoIT UDF within Java
I solved it by compiling the script into an .exe
(right click and compile with options and then choose .exe and compile.
using System.Diagnostics;
Process.Start("path");
Example:
Process.Start("C:\\Users\\YourUserName\\Downloads\\hello.exe");
ProcessStartInfo psi = new ProcessStartInfo("java.exe", " -jar \"C:\\craFVUsubsreg-lite_APY.jar\"");
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
I want to invoke the utility tool, which is of java.jar file within the "c#" [mvc] application and it is work fine when i run the application through code. However after i published the application and then i try to invoke the same jar file it is not responding or i can say it is not working.
Above is the process code which i'm using to invoke the jar file. Even the user permission i have setted to full control mode.
Kindly guide me, where i am wrong, or is their any way to directly invoke the java.jar file without converting to java.exe.
I have create c# dll which is contained in web directory of the product. This dll contain a function which accept some parameters and call a exe (phantomjs.exe) with accepted parameters.I also have write some logging code for file writing in this function. Now I call this dll from asp classic code. Then I found that log maintain by this dll but work of phantomjs.exe does not occur. While when I call this dll from console application then phantomjs.exe works.
I use below code to call the exe
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = pDirectory + "\\res\\bin\\phantomjs.exe";
p.StartInfo.Arguments = arguments;
p.Start();
p.WaitForExit();
Please tell me will there be a need to sign this phantomjs.exe to run in webdirectory or any thing else.
I am writing a small utility to execute svn commands using c# program
Here is the key line in my program
System.Diagnostics.Process.Start("CMD.exe", #"svn checkout C:\TestBuildDevelopment\Code");
As I assume, the above code should be able to do svn check out and download all the code to the local path mentioned above. But what's happening is that, the command prompt opens up with the default path of the c# project and does nothing.
If I run this svn command in command line it works fine. But when I run using C# it just pops up the command prompt without executing the svn checkout operation. Any idea on what is going wrong?
You don t have to run CMD.exe. CMD.exe ist just a program that calls other assemblies.
You can call svn.exe directly with your argument checkout ... (but isnt there a url missing?)
Process.Start("svn.exe", #"checkout C:\TestBuildDevelopment\Code");
you may also try this:
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "svn.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "checkout ...";
proc.StartInfo = startInfo;
if(proc.Start()) {
proc.WaitForExit();
}
You need to add the parameter /c. Try:
Process.Start("CMD.exe", #"/c svn checkout C:\TestBuildDevelopment\Code");
The parameter /c means that the cmd should execute the command and exit.
As Jon notes, svnsharp would be a good choice here; but IMO the problem is shelling cmd.exe, rather than the exe you actually want to run:
Process.Start(#"path\to\svn.exe", #"checkout ""C:\TestBuildDevelopment\Code""");
although a ProcessStartInfo would make it easier to set the working path etc.