In my application, the user can select a program:
D:/application/app.exe
I would like to execute it such that the same situation that I have to do on CMD, it will show:
C:/
then I have to do: D:
then:
D:/application/app.exe
The application can be only run on its folder for connecting with other libraries.
How can I make it possible to execute it from C# in such a way that it locate to the D:/application first and then execute: app.exe?
Thanks in advance.
See the WorkingDirectory property of ProcessStartInfo. E.g.
Process.Start(new ProcessStartInfo {
WorkingDirectory = #"D:\application",
FileName = "app.exe"
}
You can set the working directory when you start a new process:
Process.Start(new ProcessStartInfo()
{
FileName = #"D:\application\app.exe",
WorkingDirectory = #"D:\application",
//...
});
The Path class can help you parse and manipulate your input path.
Path.GetPathRoot("D:\MyApp\App.exe") --> D:\
Path.GetDirectoryName("D:\MyApp\App.exe") --> D:\MyApp
ProcessStartInfo psi = new ProcessStartInfo(#"D:\application\app.exe") { WorkingDirectory = #"C:\" };
Process.Start(psi);
Related
From a C# application I'm starting a process to run Debug.exe (...\system32\Debug.exe) using an existing file as Debug's parameter.
This is a list of StartInfo parameters I've tried:
FileName = "Debug.exe", Arguments = "CS.exe"
FileName = "cmd.exe", Arguments = "/C Debug.exe CS.exe", WorkingDirectory = "", UseShellExecute = false, RedirectStandardOutput = true
To access CS.exe I use Directory.SetCurrentDirectory(*fullname of CS*) before starting the process
But all I get is an empty window with a blinking cursor.
What am I doing wrong here?
Thanks in advance for your help
Have you tried through PowerShell?
#$script = "c:\Windows\System32\calc.exe" (location of .exe to be executed)
#$csdefFilaPath = "C:\Input.txt" (Parameter that the exe accepts/path)
Param($scriptLocation, $filePath)
function RunExe()
{
Write-Output "Running exe"
& $scriptLocaion $filePath
Write-Output "Finished Running exe"
}
Call -> RunExe
How to start process and run command like this:
mysql -u root --password="some-password" < "some-file.sql"
Is it possible to do with process.Start()?
I need cross-platform solution (we cannot use cmd.exe).
Yes, this is possible through the System.Diagnostics.Process class. You need to set RedirectStandardInput to true, after which you can write the content of a file redirect the standard input of a process, and write the contents of the file to the Process.StandardInput (which is a StreamWriter)
This should get you started:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "mysql.exe", // assumes mysql.exe is in PATH
Arguments = "-u root --password=\"some-password\"",
RedirectStandardInput = true,
UseShellExecute = false
},
};
process.Start();
process.StandardInput.Write(File.ReadAllText("some-file.sql"));
Update: this is pretty well documented [here](
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput)
I have an exe file which I run through windows command prompt and give command line arguments. I went through this post and ran the following command:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
But all it did, is to give me resource files located in WindowsFormsApplication1\obj\Debug folder
I went through this post but it tells on how to execute the exe directly without the running it from cmd.
I even tried the following command:
string path = Path.Combine(Path.GetTempPath(), "MyApplication.exe");
It worked but after clearing my C:\Users\UserName\AppData\Local\Temp folder the application started giving an error.
I even tried the following command:
global::ApplicationName.Properties.Resources.MyApplication
but it gives byte[] and not the path to the application.
All I want to know is how to run the application which is embedded in my resources so that I can successfully execute the following command:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/K " + MyApplication+" Argument "+Path1+" "+Path2 ,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadToEnd();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(resultFile))
{
file.WriteLine(line);
}
}
Extract the resource into a file in the filesystem and then run it.
byte[] fileContents = ApplicationName.Properties.Resources.MyApplication;
File.WriteAllBytes("MyApplication.exe", fileContents);
Now you can run the file using MyApplicaton.exe as path.
I'm trying to start application "GA.exe", but on start it's taking data from file "acc.txt".
If I start it normallly (via double click :-)) it works, but if I use code below it say "Can't find acc.txt".
My first idea:
Process.Start(pathToGA.exe);
Second idea:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
};
And both don't work.
You should set ProcessStartInfo.WorkingDirectory to the directory that holds acc.txt and GA.exe:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
WorkingDirectory = FolderWithGA
};
I've written a program that checks if it is in a specific folder;
if not, it copies itself into that folder,run the copied program and exit.
but the problem is when I call
Directory.GetCurrentDirectory();
in the copied program(only when It runs by the first one) I get the directory of the first program not the copied one.
What's the problem here?
the code:
if(Directory.GetCurrentDirectory()!=dir)
{
File.Copy(Application.ExecutablePath,dir+name);
System.Diagnostics.Process.Start(dir+#"\"+name);
System.Environment.Exit(System.Environment.ExitCode);
}
i summarized my codes.
You have to use the WorkingDirectory on the processinfo, no need for copying the files.
if(Directory.GetCurrentDirectory()!=dir)
{
string exepath = Path.Combine(dir,name);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
process.StartInfo.FileName = exepath;
processStartInfo.WorkingDirectory = dir;
//Set your other process info properties
Process process = Process.Start(processStartInfo);
System.Environment.Exit(System.Environment.ExitCode);
}