A parameter cannot be found that matches parameter name 'all' c# - c#

I am trying to execute a command through c#. I pass a command as a parameter through a function which then executes it on my terminal in VS Code. When I pass the command 'ps' it works, but when I pass the command 'ls - all' it gives me the error: A parameter cannot be found that matches parameter name 'all'. I think it probably has to do something with the space, but I'm not sure. I don't know how to solve it. This is the function I use to pass the command and execute it:
public void ExecuteCommand(string key) {
System.Diagnostics.Process process = new System.Diagnostics.Process();
try {
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = "/c " + key;
process.StartInfo = startInfo;
process.Start();
process.Close();
}
catch {
Console.WriteLine("Error occured");
}
Console.WriteLine("Command executed");
}

You have to use the command ls -Force to list all the files including the hidden files.ls -al works in linux.

Related

Running CMD commands from C#

First of all, i searched a lot to avoid asking a duplicate question. If there is one, i will delete this question immediately.
All the solutions on the web are suggesting to use Process.StartInfo like this one
How To: Execute command line in C#, get STD OUT results
I dont want to run a batch file, or an .exe.
I just want to run some commands on cmd like
msg /server:192.168.2.1 console "foo" or ping 192.168.2.1
and return the result if there is one.
How can i do that ?
Those commands are still exe files, you just need to know where they are. For example:
c:\windows\system32\msg.exe /server:192.168.2.1 console "foo"
c:\windows\system32\ping.exe 192.168.2.1
The only proper way to do this is to use Process.Start. This is demonstrated adequately in this question, which is itself a duplicate of two others.
However, as DavidG says, the commands are all exe files and you can run them as such.
Apparently, i found an answer
while (true)
{
Console.WriteLine("Komut giriniz.");
string komut = Console.ReadLine();
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C" + komut;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
Console.WriteLine(process.Start());
string line = "";
while (!process.StandardOutput.EndOfStream)
{
line = line + System.Environment.NewLine + process.StandardOutput.ReadLine();
// do something with line
}
Console.WriteLine(line);
Console.ReadLine();
}
seems like if you can run cmd.exe with arguments including your command.
thanks for contributing.

Start a process with a command line parameter of another executable

I'm trying to pass an .exe to another through C# code.
Here's my code so far:
string ex1 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\psm.exe";
string ex2 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\Application\\app.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = ex1;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = ex2;
try
{
Process.Start(startInfo);
}
catch
{
}
Which argument would work as dragging a file onto the application?
Details:
When you run psm.exe normally, it prompts for file name and directory.
However, when you drag an approved app on psm.exe,
it loads the app automatically.
How can this be done with C#?
You can run another app synchronously like this:
System.Diagnostics.Process myapp = new System.Diagnostics.Process();
myapp.StartInfo.FileName = ex1;
myapp.StartInfo.Arguments = ex2;
myapp.Start();
myapp.WaitForExit();
Depending on how the app you want to launch expects the command line arguments to be passed, you may need this for the arguments:
myapp.StartInfo.Arguments = String.Format("/MyArgumentName={0}", ex2);
That would be the equivalent of:
c:\MyApplicationStartupPath\dev\psm.exe /MyArgumentName=c:\MyApplicationStartupPath\Application\App.exe
Be sure to match the way app.exe expects the parameters in your StartInfo.Arguments

Working with the command prompt in C#

I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. Once I get it working locally, then I will move to remote communication.
Problem:
I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it.
I had to use c# to send a command to cmd.exe and receive the result back in C#. I have done it in one way by setting the StandardOutput... and input to true.
The commands are not working. For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories.
Here is my code:
First Method
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();
Second Method
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = #"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;
I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive.
Try not to run the commands by passing them to cmd instead write the commands passed by the client to a.bat file execute the .bat. file from your program this will probably hide your command prompt window.
You can also use process.OutputDataRecieved event handler to do anything with the output.
If you want to execute command using administrator rights you can use runascommand. It is equivalent to the sudo command in Linux. Here is a piece of code may be it will help you
var process = new Process();
var startinfo = new ProcessStartInfo(#"c:\users\Shashwat\Desktop\test.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataRecieved += DoSomething;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
//Event Handler
public void DoSomething(object sener, DataReceivedEventArgs args)
{
//Do something
}
Hope it helps you.
You could hide command prompt window by adding this line of code:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
or do not create it at all
startInfo.CreateNoWindow = true;
Here can be found a few awarding solutions:
Run Command Prompt Commands

Execute a Batch File From C#

I have a small problem. Okay let's say from my C# console application I want to run a batch file that will take an argument. The string variable at the stop of my C# application will be the string argument to pass to the batch file. How would I go about doing it?
Here is my code so far my C# console program:
//String argument to pass to the batch file
string message = "Hello World";
System.Diagnostics.Process process = new System.Diagnostics.Process();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Greetings.bat";
startInfo.Arguments = "/C " + message;
process.StartInfo = startInfo;
process.Start();
My Batch File
CLS
#ECHO OFF
ECHO %1
You can give argument like this.
ProcessStartInfo psi = new ProcessStartInfo(filePath);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.Arguments = "value1";

Calling Mp4Box.exe from .NET C#

Hello i am making a code for doing some editing on mp4 video using mp4box.exe
i want to execute this command line:
"D:\Work\Me\CloudContentUpload\trunk\ContentUploading Current\bin\Debug\Mp4Box\Mp4Box.exe" -isma -inter 500 "C:\Users\Abdullah\Desktop\videoo\amr khaled - Asmaa_elmogeb\Asmaa_elmogeb(1).mp4"
This command executed successfully when i run it manually on command line
but i try to execute it with the following C# code:
public string ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
return result;
}
catch (Exception objException)
{
return objException.Message;
}
}
the result returned is empty string !!
You shouldn't need to call cmd for this.
You should call your program directly and pass in the arguments to the Arguments property of ProcessStartInfo.

Categories

Resources