This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Process.Start default directory?
I have a C# application, mono to be specific. This application needs to launch another application on the users system. I know of Process.Start and how to use it, but theres something pecuilar about this instance which makes that not work correctly.
For some reason the program I am trying to launch via Process.Start needs to be called from the directory it resides in, otherwise it gives an error on opening.
What I mean by that is, if I open up a command prompt and type in:
C:\appFolder\app.exe
The application will then give me an error.
However if I open a prompt and go:
cd c:\appFolder
app.exe
It then launches just fine.
The problem I am having with process.start is it tries to open the application without first doing what is the equivalent of 'cd c:\appFolder', and so the application gives an error on opening.
So how can I do make Process.Start do what would be the equivalent of first navigating to the apps folder 'cd c:\appFolder' and then calling app.exe?
BTW, I have solved this problem by putting
cd C:\appFolder
app.exe
into a .bat file, and have Process.Start open the .bat file, which works just fine. But I am curious to know if there is a way to cut out the .bat file.
Using cd blah just changes your working directory. You can set the working directory of your process by setting the WorkingDirectory of your ProcessStartInfo. Perhaps something like this:
var procInfo = new ProcessStartInfo("app.exe");
procInfo.WorkingDirectory = #"C:\appFolder";
Process.Start(procInfo);
try changing the working directory before your call
Directory.SetCurrentDirectory(#"path");
http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx
var psi = new ProcessStartInfo("app.exe");
psi.WorkingDirectory = #"C:\appFolder";
Process.Start(psi);
Use a ProcessStartInfo object to start the application and set the WorkingDirectory property accordingly.
Related
This is a silly and tricky issue that I am facing.
The below code works well (it launches Calculator):
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = #"c:\windows\system32\calc.exe";
Process ps = Process.Start(psStartInfo);
However the below one for SoundRecorder does not work. It gives me "The system cannot find the file specified" error.
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = #"c:\windows\system32\soundrecorder.exe";
Process ps = Process.Start(psStartInfo);
I am able to launch Sound Recorder by using Start -> Run -> "c:\windows\system32\soundrecorder.exe" command.
Any idea whats going wrong?
I am using C# in Visual Studio 2015 and using Windows 7 OS.
UPDATE 1: I tried a File.Exists check and it shows me MessageBox from the below code:
if (File.Exists(#"c:\windows\system32\soundrecorder.exe"))
{
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = #"c:\windows\system32\soundrecorder.exe";
Process ps = Process.Start(psStartInfo);
}
else
{
MessageBox.Show("File not found");
}
Most likely your app is 32-bit, and in 64-bit Windows references to C:\Windows\System32 get transparently redirected to C:\Windows\SysWOW64 for 32-bit apps. calc.exe happens to exist in both places, while soundrecorder.exe exists in the true System32 only.
When you launch from Start / Run the parent process is the 64-bit explorer.exe so no redirection is done, and the 64-bit C:\Windows\System32\soundrecorder.exe is found and started.
From File System Redirector:
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64.
[ EDIT ] From the same page:
32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32.
So the following would work to start soundrecorder.exe from the (real) C:\Windows\System32.
psStartInfo.FileName = #"C:\Windows\Sysnative\soundrecorder.exe";
Old thread but providing one more possible case
In my case i was using arguments inside Process.Start
System.Diagnostics.Process.Start("C:\\MyAppFolder\\MyApp.exe -silent");
I changed it to
ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe");
info.Arguments = "-silent";
Process.Start(info)
Then it worked.
One more case, similar to Ranadheer Reddy's answer, but different enough to trip me up for awhile.
I was making a simple mistake. I had this:
ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe ");
info.Arguments = "-silent";
Process.Start(info);
See that space after the end of the path to the app? Yeah. It doesn't like that. It will not find your file if you include that.
The solution was to remove the extraneous space. Then it worked.
This is an easy enough mistake to make if you're converting an app from starting processes by launching "cmd.exe /c MyApp.exe -silent" to running "MyApp.exe" directly instead, which is what I was doing. I hope recording my misfortune here will help future developers.
I got PythonApplication1.py into the Sources of the C# Windows Form Application. On specific condition I write it into the Program Data folder this way:
File.WriteAllBytes(#"%ProgramData%\\Folder\\PythonApplication1.py", Resources.PythonApplication1);
and then with specific condition C# runs Python Application from program data folder, this way:
Process.Start(#"%ProgramData%\\Folder\\PythonApplication1.py");
On the side of Python code I got second thing, it writes some data into the text document, which is located by the same path, but creation of it happens in Python itself, but it can be done from C# code, no matter, here:
path = (#"%ProgramData%\Folder\doc.txt")
but python code must write in this file by same program data path:
data = open (r'doc.txt', 'w')
and to write, just by same location, as both are there:
with open(r'doc.txt') as my_file:
The problem is, when I run this python code as it shown above, this way:
Process.Start(#"%ProgramData%\\Folder\\PythonApplication1.py");
It does two wrong things, which does not happens, if it is not %ProgramData% directory and located with C# .exe in debug folder, just by Process.Start("PythonApplication1.py");, or if I run Python Code inside %ProgramData%\Folder path with doc.txt, just by hand. It creates and writes directly into the file, all is correct.
Otherwise if Process.Start(#"%ProgramData%\\Folder\\PythonApplication1.py"); I got second result, it does not writes data into the doc.txt by %ProgramData%\Folder and python code creates it out of program data folder in debug folder with C# .exe.
So question is how to create and write from python code into the doc.txt located by C# Application "%ProgramData%\Folder" path.
Seems like, if PythonApplication1.py is exist inside ProgramData:
File.WriteAllBytes(#"%ProgramData%\\TAOZ\\PythonApplication1.py", Resources.PythonApplication1);
And if executed by hand it creates text document and writes to it, it must do the with Process.Start, but not.
also I tried to use star info, but not sure if this is correct, anyway I got same result:
ProcessStartInfo startInfo = new ProcessStartInfo(#"%ProgramData%\\folder\\PythonApplication1.py");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
So seems like I need two things here. To expand the search environment variable %ProgramData%. and also need to use the start up info to tell the process to start on that particular folder. Otherwise, the current working folder will be the same folder as application I guess, but not sure how to do it, and if it is reason of problem, need your help.
Since it's a python program, you'll have to point to the python EXE. Assuming you have the pythonpath environment variable, this ought to work:
ProcessStartInfo startInfo = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(#"%PYTHONPATH%\python.exe"), Environment.ExpandEnvironmentVariables(#"%ProgramData%\folder\PythonApplication1.py"));
startInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(#"%ProgramData%\folder\");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
This question already has answers here:
Set environment variables for a process
(2 answers)
Closed 7 years ago.
I am currently working on a program that would run other application under given virtual environment. I am running another application like so:
Process app = new Process();
app.StartInfo.FileName = #"W:\path\to\app\some.exe";
app.EnableRaisingEvents = true;
app.Start();
Now I have faced an issue to change some system variables for application to be run. I have googled for this and could not found the solution. Anybody has any idea how to solve this problem, please help me?
Thanks.
Update
For example I want to set another PATH, JAVA_HOME, AppData variable for child application. Application could be: Google Chrome, Notepad++ or simple .bat script for command line.
assuming you need to change path variable
string pathvariable=Environment.GetEnvironmentVariable("Path");
Environment.SetEnvironmentVariable("Path",pathvariable+";"+"*you new value of path variable*");
if your exe file works fine when you access it directly then you dont need to change the your system variable.. if you change your working directory while accessing the exe file then i guess it will work
I have a C# program. It is literally one line:
System.Diagnostics.Process.Start(#"C:\ProgramData\task manager\killtask.vbs");
There is a VBS file there, which generates a batch file that allows you to type a command and it will be executed and closed. It then sends the following keys: "tskill /a notepad {ENTER}". I know that's probably the worst practice you've ever seen, but bear with me.
When the VBS file is run by hand, it successfully closes notepad. When it is run through C# using the above line, it prints "tskill is not recognized" etc. before it closes.
Why is it that I can't use tskill through batch via VBS via C#, but I can use it just through batch via VBS? Remember, both clicking on it and running my C# code successfully ATTEMPT to kill notepad, but only clicking on it by hand closes notepad successfully.
You probably need to set the working directory. Without it your program will be executed from the directory of the process that starts your script and this directory is not the correct one.
Juse try with
var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = #"C:\ProgramData\task manager");
processStartInfo.FileName = "killtask.vbs";
Process proc = Process.Start(processStartInfo);
I have a c# program through which i am opening cmd window as a a process. in this command window i am running a batch file. i am redirecting the output of that batch file commands to a Text File. When i run my application everything seems to be ok.
But few times, Application is giving some error like "Can't access the file. it's being used by another application" at the same time cmd window is not getting closed. If we close the cmd process through the Task Manager, then it's writing the content to the file and getting closed. Even though i closed the cmd process, still file handle is not getting released. so that i am not able to run the application next time onwards.Always it's saying Can't access the file. Only after restarting the system, it's working.
Here is my code:
Process objProcess = new Process();
ProcessStartInfo objProInfo = new ProcessStartInfo();
objProInfo.WindowStyle = ProcessWindowStyle.Maximized;
objProInfo.UseShellExecute = true;
objProInfo.FileName = "Batch file path"
objProInfo.Arguments = "Some Arguments";
if (Directory.Exists(strOutputPath) == false)
{
Directory.CreateDirectory(strOutputPath);
}
objProInfo.CreateNoWindow = false;
objProcess.StartInfo = objProInfo;
objProcess.Start();
objProcess.WaitForExit();
test.bat:
java classname argument > output.txt
Here is my question:
I am not able to trace where the problem is..
How we can see the process which holding handle on ant file.
Is there any suggestions for Java and .net interoperability
In situations like this I start up the Process Explorer ( by Sysinternals, awesome tool btw ) click Ctrl+F, and enter the name of the file. It will search across all running processes and will list the file handles to this file by the applications that have it open.
You can then either drop the handle, or kill the app - whatever you think is better )
You can try forking and attaching file descriptor from C# rather than launching a bat file.
I think the problem is because the java program is accessing the text file when the C# program is writing something on it, and hence a "file cannot access" problem.
If I were you, I would do everything in C#-- I won't use Java to read the state of the C# program. And I would access the file only after I've completed whatever the C# needs to do.
As for to see what process is locking up your file, you can use Process Explorer for this purpose.