When I execute an exe file (PVFProject15.exe), it reads the data from an input file (inputFile.txt) and print the results in another file (outputFile.txt). The exe file works well when I double click it; It opens the console window which stays opened until the output file is created. However, when I run (PVFProject15.exe) from c#, the console window opens and closes very quickly and the output file is never created.
I would really appreciate your help since I have been working to fix this for a whole day and never found the answer. Here is my code below.
private void button1_Click(object sender, EventArgs e)
{
Process runFortran = new Process();
try
{
runFortran.StartInfo.FileName = "C:\\temp\\trial\\PVFProject15.exe";
runFortran.Start();
runFortran.WaitForExit();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Thank you in advance.
Safaa
Probably PVFProject15.exe needs current directory to be set to C:\temp\trial
If PVFProject15.exe writes to file using relative path, look for outputFile.txt in directory from which you start your main program-bootstrapper.
I also meet with same problem, when I try start some .exe and .hta from my C# based software.
I start to looking for solution and answer of Mike Mozhaev get to me right direction.
In your code you need to use:
StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
So code have to be like this:
if (File.Exists(appPath))
{
Process runProcess = new Process();
runProcess.StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
runProcess.StartInfo.UseShellExecute= true;
runProcess.StartInfo.FileName = appPath;
runProcess.Start();
}
Related
I've created a windows service project that will use the TiffCP exe in order to split any tiff files its found into multiple tiff files. I'm using the code that was given as an example on the site:
public static class SplitTiffImage
{
public static void Main()
{
string[] arguments =
{
#"Sample Data\multipage.tif,1",
"SplitTiffImage_2ndPage.tif"
};
TiffCP.Program.Main(arguments);
Process.Start("SplitTiffImage_2ndPage.tif");
}
}
This works as expected and splits the file. However, a process is created (MSPVIEW.EXE) and I'm unable to access the file because it is being edited in another program. I have to manually kill the process to access it. I've also tried creating the process as a variable and then trying to close or kill it but that doesn't seem to be working either. Any ideas? Thanks.
Edit: I've put this code before accessing the process again and when the service stops and it seems to do the trick. It works but I'm wondering if there is a better way.
Process[] process = Process.GetProcessesByName("MSPVIEW");
if (process.Length > 0)
{
foreach (var p in process)
{
p.Kill();
}
}
Remove the
Process.Start("SplitTiffImage_2ndPage.tif");
from the code above.
The line opens the output in a default viewer. In your case it's Microsoft Office Document Imaging (MSPVIEW.EXE). You clearly don't need the output to be opened in a viewer.
I have an issue with an auto update software that I am trying to make. The application runs as a server application and checks via FTP for updates and downloads them if there is a newer version available. This then unzips a folder called update in the programs root directory. it then launches a file called update.bat that does any file copying etc that I may need to do for that update. Once this is finished the update.bat launches the new server application. Once the program goes to check for updates again it is suppose to delete the update directory that is in the root directory of the server application as well as the update.rar file that was downloaded from the update server. All of this works perfectly except the folder is being used and will not delete. I have read all kinds of things about releasing the handle and changing the current directory etc.. but just can't seem to get it to work. I would appreciate someone helping me out here. Here is the code for this update.
private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in Directory.GetDirectories("C:/my update dir"))
{
if (s.Contains("Instance"))
{
var _instance = Regex.Match(s, #"\d+");
Process p = new Process();
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.FileName = "cmd.exe";
pinfo.WorkingDirectory = "C:/mySQL/bin";
pinfo.Arguments = "/C mysql.exe -u** -p** dbnameHere" + _instance.ToString() +
" < \"C:/my update dir/update/update.sql\"";
p.StartInfo = pinfo;
p.Start();
p.WaitForExit();
p.Close();
p.Dispose();
Directory.SetCurrentDirectory("C:/");
}
}
Directory.SetCurrentDirectory("C:/");
this.Dispose();
Application.Exit();
}
I'm going to guess that your problem is here:
Once this is finished the update.bat launches the new server
application.
Windows is going to "lock" the directories all the way down to the .BAT file. So if the .BAT runs a server process from that directory, that process is going to inherit the CWD and file descriptors of the calling process.
It's not clear from your code that this is what's happening, but you may also want to try changing the CWD prior to spawning the process.
Given the code. when the button is clicked nothing happens , i get no debug message etc in visual studio. However if i were to double click the .jar file in its folder i am able to run it. Anyone have any idea why?
Looking at Task manager when the button is clicked. javaw.exe is created but nothing happens.
private void btnKinderPuzzle_Click(object sender, RoutedEventArgs e)
{
// Check if this program is opened
if (IsProcessOpen("MTPuzzle"))
{
MessageBox.Show("KinderPuzzle is already running", "Kinder Package", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Process.Start(Directory.GetCurrentDirectory() + "\\Puzzle\\PuzzleGame\\MTPuzzle.jar");
}
}
Process.Start("java.exe",
Path.Combine("-jar " + Directory.GetCurrentDirectory(),
"Puzzle\\PuzzleGame\\MTPuzzle.jar"));
The path may be not correct. You might use instead
Process.Start(Path.Combine(Directory.GetCurrentDirectory(), "Puzzle\\PuzzleGame\\MTPuzzle.jar"));
If stills, then I think the problem in the setting of Java. To solve this potential problem, create a file run.cmd near your jar file and write this code into:
java -jar "MTPuzzle.jar"
Then, use Process.Start to start the file run.cmd
Hoping this question will get answered. Basically I am attempting to open an executable file from within an application I have created which runs on windows ce5 on a unitech barcode scanner using the .net compact framework 3.5. I have included a snippet of code where I attempt this.
Each time I debug the app via VS2008 I am getting a Win32Exception but with no further details (with or without the try catch statement). It does not tell me what the exception is nor does it provide an error code.
Here is the code I am using to try to start the process. Can you see anything wrong with it that may be causing an error? I have double and triple checked the filename and also the directory where it is stored so it is not that.
private void CustomButtonEvent(object sender, EventArgs e)
{
string buttonName = ((Control)sender).Name;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
buttonName = buttonName.Remove(0, 3);
buttonName = buttonName.Replace(' ', '_');
switch (buttonName)
{//todo need to open the different exe's here
case "End_Of_Line":
{
MessageBox.Show(#"No app behind this button yet.");
break;
}
case "Asset_Tracking":
{
processStartInfo.FileName = "AssetTrackingScanner.exe";
processStartInfo.WorkingDirectory = #"\Flash Storage\CompoundingManagementScannerSuite\Applications\AssetTrackingScanner\AssetTrackingScanner\bin\Debug";
try
{
Process.Start(processStartInfo);
}
catch (Exception f)
{
MessageBox.Show(f.ToString());
}
break;
}
case "Stock Take":
{
MessageBox.Show(#"No app behind this button yet.");
break;
}
case "Despatch":
{
MessageBox.Show(#"No app behind this button yet.");
break;
}
}
}
I see two problems. First, CE requires fully qualified paths, so the processStartInfo.FileName should be something like this
processStartInfo.FileName =
#"\Flash Storage\CompoundingManagementScannerSuite\Applications\AssetTrackingScanner\AssetTrackingScanner\AssetTrackingScanner.exe";
Second, CE has no concept of a WorkingDirectory, so remove the call to set it.
I'm also a little concerned about the \bin\debug\ part of your path. Studio doesn't deploy to a bin\debug\ folder on the device. It build to one on the PC, but on the target device the only way it would be there is if you manually set it. This leads me to think you need to check your on-device applicatin path.
I have windows forms application wich runs another console application
here is the part of code
prog = new Process();
prog.StartInfo.FileName = exefile;
The console application should create file but when running that application from C# it doesn't creates any file
when im running console application with double click it works fine
here is the part of code from "exefile" (its on c++)
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
printf("somedata\n");
"file.in" surely exists
The most likely thing is that you need to set the working path:
prog.StartInfo.WorkingDirectory = ...
i.e. I'm thinking it can't find file.in in the current app folder.
You need to add this line whenever you want to start the process:
prog.Start();
Here is the link to the MSDN page for Process.Start. There are several overloads that you may want to consider.
I would suggest,
handle exceptions to see what's going wrong
like mentioned before make sure you
call the start() method
Here's a snippet of code from msdn, that you might want to refer
Process myProcess = new Process();
try
{
// Get the path that stores user documents.
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc";
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message +
". You do not have permission to print this file.");
}
}