Open File from PowerShell via c# - c#

In PowerShell this command: Invoke-Item D:\myFile.txt will open the file named myFile.
I'm tryind to run this PowerShell command From C#
I tried:
NuGet: System.Management.Automation
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Invoke-Item D:\tx.txt");
PowerShellInstance.Invoke();
}
But this not opens the file

You can use this code:
Process process = new Process();
string scriptPath = $"YourPowershellCommand.ps1";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = scriptPath,
RedirectStandardError = true;
RedirectStandardOutput = true;
CreateNoWindow = true;
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
var stderrx = process.StandardError.ReadToEnd();
var stdout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Just save your powershell command in a ps1 file and give the path as a parameter. The code is not tested but it should work. Also C# process is in System.Diagnostics namespace.

Related

C# Programmatically opening a cmd.exe and writing a command into console when UseShellExecute is true

I've tried the snippet of code:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = true;
info.Verb = "runas";
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0");
}
}
It successfully opens up a command prompt with administrative privileges but I can't seem to write into it use streamwriter. I know that this is because I don't have RedirectStandardInput set to true. If I do set this to true, I am able to write but my console loses its elevated privileges. My question is how can I write a command into the console when UseShellExecute is true?

Command Line Using c# Code

I am starting a simple process using my c# code. I am able to open the cmd but not in the specified path and even the command is not executed, instead it shows The Handle is invalid error. It is done using simple winform application.
Below is my code:
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
WorkingDirectory = #"D:\Work\Application",
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("timeout /t 10");
It worked a day before but then it didnt work. I cant see what the problem is
All you have to do is:
string cmdText;
cmdText = "/C timeout /t 10";
System.Diagnostics.Process.Start("CMD.exe", cmdText);

How to run a command tool exe in C# web application?

I am using graphicmagic exe to execute a command using command prompt. I have added the graphicmagic exe in my application root folder. I want to execute this exe and pass the arguments through c#. How to do this? I have tried the below code:
Method: 1
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = AppDomain.CurrentDomain.BaseDirectory + "\\gm1.3.5\\gm",
Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 'D:\\Image\\FFv1\\dpx1\\1.dpx' 'D:\\Image\\FFv1\\tiff1\\1.tiff'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
}
Method: 2
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm";
startInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"D:\\Image\\FFv1\\dpx1\\1.dpx\" \"D:\\Image\\FFv1\\tiff1\\1.tiff\"";
proc.StartInfo = startInfo;
proc.Start();
But both of its not working. Please suggest a way to execute exe file and pass commands.
It should be the privilege issue.
Try to adjust the identity in your app pool to LocalSystem.
This one is working fine:
using System.Diagnostics;
Process p = new Process();
p.StartInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm.exe";
p.StartInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"C:\\Documents and Settings\\software\\Desktop\\DPX_Test\\3.dpx\" \"C:\\TEMP_21May2015103216\\3.tiff\"";
p.Start();
p.WaitForExit();

getting a blank cmd window by running command line from C#

I am trying to open command line in the c folder, from C#.
the expectation is to see this in the command line window:
C:>
but instead i am getting a blank cmd window.
this is the code:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Console.ReadKey();
WaitForExit is what you are looking for.
EDIT:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
This will halt the execution of all statements after WaitForExit. The time you close the command window, the statements following WaitForExit will be executed.
i think you're looking for this
first we'll create a process for CMD.exe and then passes "/K cd \". "/K" will "CMD.exe" to receive parameter and stay open, while "cd \" will take us to "C:/" which is your requirement
System.Diagnostics.Process.Start("CMD.exe", "/K \"cd /\"");
Console.ReadKey();
I believe if you use the /K Argument when executing the command, you should have a command prompt running at C:\
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"C:\";
startinfo.Arguments = "/K";
startinfo.UseShellExecute = false;
Process.Start(startinfo);
Or
Process command = new Process();
command.StartInfo.UseShellExecute = false;
command.StartInfo.WorkingDirectory = #"C:\";
command.StartInfo.Arguments = "/K";
command.StartInfo.FileName = "cmd.exe";
command.Start();
The /K argument executes the cmd.exe command and keeps the window open :)

redirect any results in command prompt to richtext box

I need to redirect any results in command prompt to richtext box. Can any one provide me the necessary steps. This is how i start my command prompt.
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "cmd",
Arguments = #"/k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat""",
};
Process.Start(psi);
var process = new Process
{
StartInfo = new ProcessStartInfo("cmd.exe", "/C dir c:")
{
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
EDIT: Then you can try this
var process = new Process
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
}
};
process.Start();
// Discard "Microsoft windows all rights reserved etc.
while (process.StandardOutput.ReadLine() != "") ;
// Run command
process.StandardInput.WriteLine("dir c:");
// Skip command entered
process.StandardOutput.ReadLine();
// And exit
process.StandardInput.WriteLine("exit");
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
You're going to want to redirect the stdout stream from the Process. I don't remember the exact properties and methods involved, but take a look through the MSDN documentation on Process.

Categories