Unable to run application - c#

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-

Related

Process cannot find file specified

I have the following issue:
I have a C# app that runs as a 32-bit Application on my 64-Bit Machine. This application opens a process and starts wbadmin to make a backup of the C drive.
Now: when I call enter "wbadmin" in the command line wbadmin works and shows lists all its commands.
In my c# app when i do
ProcessStartInfo info = new ProcessStartInfo("wbadmin", $"start backup -backupTarget:{destinationDrive} -include:C: -quiet -vssCopy")
{
UseShellExecute = false
};
Process p = new Process()
{
StartInfo = info,
EnableRaisingEvents = true
};
p.Start();
it tells me that the file specified cannot be found.
I did some research and I think my app is looking for the wbadmin in the WOW64 folder, where it cant find it because it is inside the System32 Folder.
How do I tell my program to use the correct wbadmin.exe in the correct location?
Do I HAVE to run the app as a 64-bit application for it to work?
If so how would i support 32-bit architecture?
Thanks
Thanks to #PavelAnikhouski for providing me with the correct answer.
I had to specify the path to sysnative.

C# executing an exe file as if it were from the exe's own folder

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();

Running batch file from C# Force run from file's directory?

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";

Cannot launch PSEXEC

I have this very simple program
Process process = new Process();
process.StartInfo.FileName = #"psexec";
process.Start();
But when I run it the debug says "The system cannot find the file specified"
If I have the same program and change "psexec" by "Notepad", it works and opens notepad.
Process process = new Process();
process.StartInfo.FileName = #"notepad";
process.Start();
This is weird because I have my psexec in the System32 and if run "psexec" using Windows-Run, it works.
Thank you in advance for any help.
Update: I specify the full path #="C:\Windows\System32\PSexec.exe" and It doesnt work. But If I move Psexec to, as example #"D:\psexec.exe" it works!!
Why coud this happen?
Running programs from c:\windows\system32 is troublesome on a 64-bit operating system. The workaround is Project + Properties, Build tab, change Platform target to AnyCPU. Or to copy the file also to c:\windows\syswow64.
Or to just not put it in the Windows directory, it is not an operating system specific file that belongs there. The appropriate place is the same directory as your EXE.
You can learn more about the File System Redirector in this MSDN article.
try specify the full path of 'psexec'
there is any property 'WorkingDirectory', which might help.

c# application doesn't perform batch file command correctly

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.

Categories

Resources