I'm trying to run a software's batch utility tool via C# and the command line but am not able to get it to work. I am able to build a string which is correct and works when I just copy and paste it into the command prompt, but when my code tries to do that step automatically nothing happens; the script just runs to the end and nothing happens.
For reference, this is what I'm trying to do: https://knowledge.autodesk.com/support/navisworks-products/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/Navisworks/files/GUID-14B017E2-B4A2-432F-8C2D-1F1856EECB8E-htm.html
I've tried a number of times changing the different properties of StartInfo, but nothing has worked so far. Could there be an issue with the different directories that are at play?
"filesTextFile" is a .txt file on the C: drive which lists paths for files located on a shared network drive (\\{network}\)
cmd.exe is in the C: drive
"NWBatchUtilityLoc" variable is the file path for an .exe on the C: drive
.cs script is running from the D: drive.
This is an excerpt of my code that shows how the string is being built and how I am using System.Diagnostics.Process.
var utilityCmd = $#"""{NWBatchUtilityLoc}"" /i ""{filesTextFile}"" /osd /log ""{logDir}"" /lang en-US";
var p = new Process
{
StartInfo =
{
FileName = #"C:\Windows\System32\cmd.exe",
Arguments = "/C " + utilityCmd,
}
};
p.Start();
Related
I need to mimic the command line version of this:
If i am in say H:\
And run:
H:\> D:\MyFolder\MyOtherFolder\TheFile.exe
This will launch TheFile.exe app, though that needs to run as if it was launched from:
D:\MyFolder\MyOtherFolder\> TheFile.exe
Anyway to get Process.Launch to execute from actual folder not the app folder thats running the code that launches the .exe?
You can specify the working directory in Process start. For example:
AppProcess = new Process();
AppProcess.StartInfo.FileName = "D:\\MyFolder\\MyOtherFolder\\TheFile.exe";
AppProcess.StartInfo.WorkingDirectory = "D:\\MyFolder\\MyOtherFolder";
AppProcess.Start();
I am trying to get the output of a process. I've achieved this by using a .bat, which contains the following text:
program.exe > output.txt < input.txt
where program.exe is my executable, output.txt is the file I want to output the data to and input.txt is just an empty file since the program wants key inputs from time to time. The .bat file works perfectly when I run it myself, but when I attempt to run it using C# code, it just doesn't finish properly.
I am trying to run it using the following code:
Process.Start(path);
I've tried a lot of different stuff but this is the code that I last tried but none of my attempts worked. Also, the .bat file doesn't run properly when you run it as an administrator and the C# program I am using is requiring administrator permissions. Could this be the issue and not the actual process running?
You need to run the process cmd.exe /c [path] (aka the process is "cmd.exe", and your arguments are $"/c {path}".
When you're on the command prompt you get "program execution" and ".bat/.cmd interactive scripting". Since you want to execute a batch file you need to tell CreateProcess that cmd.exe is what's actually running it.
In ProcessStartInfo you should set UseShellExecute to false, then set WorkingDirectory in the path where you found that the bat is working correctly.
Below is an example to make you understand better:
using (Process MyProcess = new Process())
{
var MyProcessInfo = new ProcessStartInfo()
{
UseShellExecute = false,
WorkingDirectory = "path",
FileName = "file.exe",
Arguments = "args"
};
MyProcess.StartInfo = MyProcessInfo;
MyProcess.Start();
}
The issue was just what I thought. The .bat file doesn't work properly when launched from an elevated app. To fix it I used this:
Process.Start("explorer.exe", path);
Worked perfectly. Fixed it really quickly but thought I'd leave this here if anybody finds himself stuck with the same thing.
So here's my end goal. Steam allows you to add other installed games to your library but only if it's a .exe file it's pointed at to start running.
I just installed Arena and Daggerfall and they both run via DOSBox which is launched from a .bat file. So I decided to turn it into a .exe by writing my own. So far I've got the code written. When I just run the .bat file, it opens everything fine, however, when I try running it from my code, the .bat file executes but with errors. Here is my code below:
if (File.Exists("D:\\Bethesda Softworks\\Arena\\Arena (Full Screen).bat"))
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c" + "\"D:\\Bethesda Softworks\\Arena\\Arena (Full Screen).bat\"");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
System.Diagnostics.Process proc;
proc = System.Diagnostics.Process.Start(psi);
}
And the error I'm getting is this:
"The system cannot find the path specified. 'dosbox.exe' is not recognized as an internal or external command, operable program or batch file.
I'm not sure if this is an issue because of how dosbox is called from the Batch file or how the .exe ends up running the batch file. Either way, I'd rather fix this in the code rather than by making alterations to the .bat file itself.
Any help is greatly appreciated!!
Try setting ProcessStartInfo.WorkingDirectory
psi.WorkingDirectory = "D:\\Bethesda Softworks\\Arena";
I'm testing some code which starts a process, calling an application's .exe file.
When opening it programmatically, I get the following error:
When I try loading it through the command prompt console, if I first cd to the parent directory, and then call the .exe, it works fine. Thus, I am suspecting that it is due to the parent directory.
This is my code:
Process process = new Process();
process.StartInfo.WorkingDirectory = "C:\\Test\\";
process.StartInfo.FileName = "Test.EXE";
process.Start();
I seem to be setting the parent directory - what else can I do?
The working directory is not the directory where the executable is located. It's the directory the executable considers its current directory.
Eg. all console commands reside somewhere below the Windows directory yet their working directory is whichever directory is current in the command line.
Your code uses a relative path name for the executable which means Windows will look for Test.exe in whatever directory is the current (working) directory for your parent application.
To fix this, simply pass the full path to the executable in StartInfo.FileName.
I just tested the exact code you posted here and it worked fine with me here :
private void button1_Click(object sender, EventArgs e)
{
Process myProc = new Process();
myProc.StartInfo.WorkingDirectory = "C:\\";
myProc.StartInfo.FileName = "Chat APP.exe";
myProc.Start();
}
so consider checking your system , privacy settings , permissions .. etc .
You can also start the process directly by passing path as:
Process.Start("C:\\Test.exe");
Apparently there is nothing wrong with your code. To sole this problem go through these links:
http://www.techspot.com/community/topics/please-insert-a-disk-into-drive-device-harddisk-dr1-error-when-starting-a-program.162902/
http://www.fixya.com/support/t6556978-correct_device_harddisk1_dr7_problem
http://social.technet.microsoft.com/Forums/windowsserver/en-US/2e3d2e68-a4e7-49b7-9d70-7b70c166a1c9/how-to-find-out-which-hdd-is-deviceharddiskdr-
public void runBatchfile(String batchfilename)
{
try
{
ProcessStartInfo processInfo = new ProcessStartInfo(batchfilename);
processInfo.UseShellExecute = false;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.StartInfo.CreateNoWindow = true;
batchProcess.Start();
batchProcess.WaitForExit();
}
catch (Exception r) { }
}
runBatchfile(#"c:\lol.bat");
lol.bat contains these 2 lines
dir c:\ /s /b > c:\filelist.txt
exit
and when I run my code all it does is creating a filelist.txt file, but doesn't actually perform the rest of the command, which does work if I manually insert it into CMD.
Btw I've tried making the extension .cmd and I also tried without the exit command, without any other results.
please help me :)
On my Windows 7 64-bit machine with Visual Studio 2010, running this code straight from the IDE doesn't do anything whatsoever.
On a hunch that it might have something to do with permission to write to the root directory of drive C:, I tried running the .exe directly from an Explorer window with admin rights (right-click, Run as Administrator) and that worked.
I'd say it's a permission problem.
Maybe you could redirect the output to a file located elsewhere?
update:
I changed the batch file so that the dir command gets redirected to my desktop and it runs just fine.
I've had issues with this as well when calling from C#. The workaround that I usually use is to physically allow it to show the window when running the command. Not sure why this makes a difference but it has worked for me in the past.