how to create command application like run command for vista - c#

how to create application like window run command using C#. When i insert any command (for example: ipconfig) , this return result (for example: 192.168.1.1) on the textbox.
how to get windows command list?
how to get command result?
how to get installed application list on the machine?

(1) The command list will most likely come from whatever executables are found in your %PATH%. You can figure out your list by finding all .exe/.bat/other executable files in every folder specified by %PATH%. You may not even need to know which apps are available, the Process.Start method will find them for you. (see below)
(2) You can run a command-line tool programmatically using:
System.Diagnostics.Process.Start("notepad.exe"); // located using %PATH%
To capture the output, you have to redirect it like this:
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"ipconfig");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process myProcess;
myProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = myProcess.StandardOutput; // Capture output
myProcess.WaitForExit(2000);
if (myProcess.HasExited)
{
string output = myOutput.ReadToEnd();
Console.WriteLine(output);
}
(3) Probably the same answer as 1

Create a Windows Forms application using the wizard. Draw a text box and a button. Add a Click handler to the button which takes the contents of the text box and launches a process. Use the Process class. That class also has a StandardOutput property that you can read the output from so you can put it into the text box.
You may find that to use many Command Prompt commands, you need to type CMD /C in front, because they aren't separate programs from the command interpreter.
As for discovering a list of commands, that's not generally possible. A command is just a program (or a feature of the CMD command interpreter). You could search the hard drive for .exe files, but then many of them wouldn't be suitable as "commands".

Related

Process.start with application & filename as variables

Have been searching, but surprisingly could not find this specific question:
With C# I want (by clicking a button in a form) to run a certain file, with an certain application.
When using "Process.start(variable)" I can only pick one of the two.
And by using "Process.startinfo.filename" (like: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0) this also seems to be the case.
Isn't is possible to just combine both in some "easy" way?
Thanks.
Typically you would run a file with an application using a command argument (i.e. 'notepad.exe file.txt').
If that is possible with the application(s) you are attempting to launch, then you would simply need to set the Filename property of StartInfo to the name, if in the PATH, or the full path of the application and the Arguments property to the path of the file.
var process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\\{pathToFile}\\file.txt";
process.Start();
The above code would launch notepad opening file.txt. You can simply replace the FileName and Arguments with variables containing the paths to the application and file.

How to run and interact with a batch file in a Windows Forms Application?

I'm trying to write a front end application that makes use of existing tools (that are run from batch files) to create bootable media for a Ryzen Hackintosh. I don't want to modify the existing tools as I would have to manually update the front end every time the existing tools get updated. I can run the batch files on their own with "Process.Start(file);" but the batch files require inputs, and I'd like to input to them programmatically, rather than give the user instructions on what to do when the cmd window pops up.
I've tried this style of running the file with cmd as a new process and redirecting input + output ( Executing Batch File in C# ), but this doesn't seem to launch cmd at all, and hard-locks the program with no error messages.
I have also tried using AllocConsole/FreeConsole too.
static void ExecuteCommand(string command, string input)
{
Console.WriteLine("Test if this thing works");
Process pro = new Process();
pro.StartInfo.FileName = command;
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.Start();
string result = pro.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
I expect the batch file to run, then print the contents of the output to the console, but instead no window appears, the batch file doesn't appear to run and the program freezes with no error messages. I have also used try/catch to see and errors but none appeared.

C# - Open a file with the associated program without invoking the command line

Is it possible to open a file with the default program without invoking the command line? I want to run a unit test and have the unit test open the file (PDF) at completion for visual inspection.
Just call Process.Start(filePath).
This will open the file in the user's default program.
I think this should work:
System.Diagnostics.Process.Start(#"c:\file.pdf"); //i.e provide the full path!
Simply use the following syntax:
System.Diagnostics.Process.Start(#"c:\yourfile.txt");
Process process = new System.Diagnostics.Process();
process.EnableRaisingEvents = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = filePath;
string arguments = fileArguments;
process.StartInfo.Arguments = fileArguments;
process.Start();
process.WaitForExit();
This way you can invoke and put the file name in the pdf with parameters/arguments. You can also specify different programs and put it in the path, then the pdf name in the fileArguments. It's up to you.
if you use this code:
System.Diagnostics.Process.Start( "C:\...\...\myfile.pdf" );
the pdf should get opened by the default program associated to the .pdf extension.
is this what you wanted? I would be careful in putting this inside the unit test in case you include those tests in an automated build on the server, which runs with no logged in user, this could be an issue if it fails and if it does not fail, who is there to close Acrobat Reader? :D

Execute OS Command on a file C#

I am trying to execute a OS command through C#. I have the following code taken from this webpage:
//Execute command on file
ProcessStartInfo procStart =
new ProcessStartInfo(#"C:\Users\Me\Desktop\Test\System_Instructions.txt",
"mkdir testDir");
//Redirects output
procStart.RedirectStandardOutput = true;
procStart.UseShellExecute = false;
//No black window
procStart.CreateNoWindow = true;
//Creates a process
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//Set start info
proc.StartInfo = procStart;
//Start
proc.Start();
but when I attempt to run the code I get the following error:
{"The specified executable is not a valid application for this OS platform."}
What am I doing wrong? I have tried this example as well but got the same issue.
The overload of the ProcessStartInfo constructor you are using expects an executable file name and parameters to pass to it - a .txt file is not executable by itself.
It sounds more like you want to execute a batch file with commands within the file. For that check this SO thread: How do I use ProcessStartInfo to run a batch file?
Try setting the USESHELLEXECUTE member to TRUE instead of FALSE.
It worked for me - but I think this has reprocussions for certain users after publishing.
You are trying to execute a TXT file. That's why you get
{"The specified executable is not a valid application for this OS platform."}
Because, well, the specified executable (TXT) is not a valid application for this OS platform.
You would target an executable or other file that has a specified opening application. You're targeting a text file; what you should do is target Notepad, and then supply the path to your text file as an argument:
ProcessStartInfo info = new ProcessStartInfo
{
FileName = "C:\\Windows\System32\\notepad.exe",
Arguments = "C:\\Users\\Me\\Desktop\\Test\\System_Instructions.txt"
}
new Process.Start(info);
Alternatively, if you mean for your text file to be executed, it needs to be made a .bat file.
You are trying to execute this:
C:\Users\Me\Desktop\Test\System_Instructions.txt mkdir testDir
The shell has no clue how to "execute" a text file so the command fails.
If you want to execute this text file as a batch file, change file extension to .bat so the system understands it's a batch file, and then set UseShellExecute so it does the default action for it (= runs it, in case of a batch file).
If you want to open up the file in Notepad, use:
ProcessStartInfo procStart =
new ProcessStartInfo("notepad.exe", #"C:\Users\Me\Desktop\Test\System_Instructions.txt");
If you want to write into the file :
//In case the directory doesn't exist
Directory.CreateDirectory(#"C:\Users\Me\Desktop\Test\);
using (var file = File.CreateText(#"C:\Users\Me\Desktop\Test\System_Instructions.txt"))
{
file.WriteLine("mkdir testDir");
}
If you have commands in the text file that you want to execute, just rename it to .bat and it should work (and presumably the contents do something with "mkdir testDir" as a parameter?)
What are you trying to accomplish?
Create a directory? Use the "System.IO.Directory.CreateDirectory" method.
Open a .txt file with associated program? Use ProcessStartInfo(#".\filename.txt") with UseShellExecute set to true. This will cause the associated program for that file type to be executed, which might not be notepad.txt.

automatic execution of sqlplus scrpts, use set lines, spool commands to create an out put file with the required layout using c#

I'm developing a c# application to automatically execute sqlplus. I would like to find a way to use the set lines, set pages.. and spool command to create an output file of the proper format and layout.
here is a code i used to run sqlplus from cmd using c#
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "sqlplus.exe";
processInfo.Arguments = "username/password#database #scriptFilePath";
Process process = Process.Start(processInfo);
how can i incorporate and use those commands i mentioned above? Help!
Can't you just set the ProcessStartInfo.RedirectStandardInput property to true and then write to Process.StandardInput?
How about:
Bring the window to front
Send the keys
Like following:
Process sqlplus = Process.Start(processInfo);
sqlplus.WaitForInputIdle();
IntPtr hWindow = sqlplus.MainWindowHandle;
ShowWindow(hWindow, 1 /*toggle*/);
SendKeys.SendWait("SELECT * FROM...");
SendKeys.SendWait("{Enter}");

Categories

Resources