From C#, open an arbitrary application - c#

Related question [stackoverflow] here.
I'm trying to do the above, but I want to take the process one step further. I want to open an arbitrary file using the default editor for the file type. From that point, I want to allow my user to interact with the file as they would normally, or continue to work in my application. The extension is what happens after the user finishes editing. Is there a way I can capture a close (and ideally save) event from the external application and use that as a trigger to do something else? For my purposes, tracking the closing of the external application would do.
I can do this in the specific case. For example, I can open a Word instance from my application and track the events that interest my application. However, I want to de-couple my application from Word.I want to allow my users to use any document editor of their choice, then manage the storage of the file being edited discretely behind the scenes.

You can do this in a manner similar to the referenced question, but the syntax is slightly different:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo =
new System.Diagnostics.ProcessStartInfo("C:\...\...\myfile.html");
process.Start();
process.WaitForExit(); // this line is the key difference
The WaitForExit() call will block until the other application is closed. You would use this code in a separate thread so that the user can keep using your application in the meantime.

Use the FileSystemWatcher class to watch for changes to the file.
EDIT: You can also handle the Exited event of the Process object to find out when the program is exited. However, note that that won't tell you of the user closes your file but doesn't exit the process. (Which is especially likely in Word).

To listen for file change, you can use the FileSystemWatcher and listen for a change in the last modified date.
You can also monitor the process and check then file when the process close.

I found this useful tip online just now. It seems to be what you are looking for. This article (link broken) has some more detail and useful, to-the-point tips on C# programming.
string filename = "instruction.txt";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#filename);
System.Diagnostics.Process rfp = new System.Diagnostics.Process();
rfp = System.Diagnostics.Process.Start(psi);
rfp.WaitForExit(2000);
if (rfp.HasExited)
{
System.IO.File.Delete(filename);
}
//execute other code after the program has closed
MessageBox.ShowDialog("The program is done.");

Related

Run an External Program or Batch File From a C# Xamarin Program

I want to have my C# (Xamarin) program run an EXE or batch (BAT) file. The user will be running my program, and will click on one of several buttons, some of which open Web pages and others of which run external programs. These files will be on the same computer as the one running the main program and don't need greater permissions. The overall program will be in Windows, UWP.
I already have code to pull info from the database saying "the button the user clicked references a program and it's (eg) C:\Tools\MyTool.exe". (Real path more like (C:\Users\Me\source\repos\ProductNameV2\ProductName\ProductName.UWP\Assets\EXE\whatever.exe".) I used a "demo.bat" file containing nothing but echo and pause statements, or references to a built-in Windows program like Notepad or Calc that an ordinary command prompt can recognize without an explicit path (ie. that's part of the recognized system Path). Yes, the real path to the dummy file does exist; I checked. I've also explicitly added files demo.bat and dummy.txt to my C# project.
Here's roughly what I've tried so far to actually run a batch file, or an EXE, or just to try opening a text file. Nothing works.
1)
bool check = await Launcher.CanOpenAsync(#"file:///C:\Tools\demo.bat"); // Returns false.
bool check = await Launcher.CanOpenAsync(#"file:///C:\Tools\dummy.txt"); // Returns true.
await Launcher.OpenAsync(#"file:///C:\Tools\demo.bat") // Seems to do nothing; silently fails.
await Launcher.OpenAsync(#"file:///C:\Tools\dummy.txt") // Same.
2)
Process batchProcess = new Process();
batchProcess.StartInfo.FileName = #"file:///C:\Tools\demo.bat"; // Same result with notepad.exe
batchProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
batchProcess.Start();
batchProcess.WaitForExit();
// Result: "Access is denied" error during Start().
3)
var otherProcessInfo = new ProcessStartInfo(#"file:///C:\Tools\demo.bat")
var otherProcess = Process.Start(otherProcessInfo);
otherProcess.WaitForExit();
otherProcess.Close();
// Result: "The system cannot find the file specified" despite it being the same path as in previous examples.
// Also tried literally using the path C:\Tools\demo.bat, without adding that to the C# project.
// One thing that slightly works is to use:
var otherProcessInfo = new ProcessStartInfo("cmd.exe", "/c echo Hello world!");
// This version opens a window and instantly closes it again. With "/c pause" instead, it opens, saying "press any key to continue".
// Chaining multiple commands with newline or semicolon characters doesn't work as a form of batch file.
So: the only tiny success I've had here is to run cmd.exe, to run a one-line command. I suppose that depending on what the batch file must do, there's some possibility of receiving a string, breaking it into lines, then running cmd.exe using method 3 to call them one at a time. Which is ugly at best.
Is there some better way to do this -- to run a batch file or an EXE from within my program?
EDIT: Yes, I did in fact look at documentation before asking. Why did I use URIs? Because of multiple errors telling me that the simple path strings ("C:\this\that") I was using were in an "Invalid URI format". Using Process.Start("notepad.exe") silently fails, doing nothing. Using a method involving System.Diagnostics.Process (found at How to run external program via a C# program? and yes I saw that before) fails with an error of "Access denied" when using my batch file reference, or silently failing (no window opens) using plain old notepad.exe. I avoided setting Process options that say hide the window.
So to rephrase: Is there a way to make my program run some EXE somewhere on the computer, or to run a batch file that has more than one command in it? What is that way?
Using the data you collected, I was able to run a batch file by doing the following:
var strPathToExeOrBat = System.IO.Path.Combine("C:\\Tools", "demo.bat");
var otherProcessInfo = new ProcessStartInfo("cmd.exe", $"/c call \"{strPathToExeOrBat\"");
var otherProcess = Process.Start(otherProcessInfo);
otherProcess.WaitForExit();
otherProcess.Close();
I also think it would be helpful to review the capabilities of the cmd.exe application.
I found this post to be helpful:
https://stackoverflow.com/questions/515309/what-does-cmd-c-mean#:~:text=%2FC%20Carries%20out%20the%20command%20specified%20by%20the%20string%20and,switches%20by%20typing%20cmd%20%2F%3F%20.
In particular the /k option will leave the window open, if you don't want it to close after running a script.
Thank you very much for your question! It really helped me find the answer to this! (at least for my situation of a .NET MAUI windows app, but MAUI is built off of Xamarin.Forms, so you shouldn't have a problem doing the same thing)
EDIT: Updated to use file path from question and string interpolation with System.IO.Path.Combine for slightly greater cross platform capability

Send signal from one winforms application to another

I have a C# Win forms application, it has UI and optionally take command line argument to execute some process. I have a new request where an external application will provide command line argument and my application need to provide progress status to the external application. I want to make minimum change to my application for this task.
For example, my application launch by external program as below:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\ProgramFolder\MyProgram.exe";
startInfo.Arguments = #"C:\arguments.txt";
Process.Start(startInfo);
While my application running, it need to send information like, account in progress, account processed, all accounts completed, etc signal.
Should my application write progress status to a predefined folder then external application use file watcher to monitor new files and get status? My research shows another option named pipes, which new to me but hopefully can manage it. Is there any other alternative approach?
It's a bit late to give you any advice...but it can be useful for someone else.
To send informations between two or more different procceses, you can use MMFs,
"Memory Mapped File"
What is? : you can find a good description by clicking Here
I'm sorry if i can't be accurate enough but i'm still studying the argument
...
you can use the MemoryMappedFile class from C# using System.IO:MemoryMappedFile

Java and .net interoperability

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.

If I have the URL of a torrent, how can I just "launch" it using the Process.Start()?

I found a way to obtain the URL of a torrent file, if I have this in string format, is there a way for me to just launch it whenever a user presses a button in my app?
I know I could save the file and then call it up, but I'd rather just open it. Is this possible?
You can just start it, but what will happen then is your default browser will open and it will download the file. And depending on the local settings on that machine it will do the default thing.
I would not recommend this method, it means the end user will have to do alot of extra steps. And the different browsers behave differently, and may not obey windows file extentions ( thing firefox )
If your doing this inside a application you should download it yourself, you can read about that here. .NET Frameworks offers great solutions to download the file yourself.
Also if you do it via Proccess you will not get a refere when downloading, some sites may then block you to stop hot linking. but if you control the download class you can send a refere url
Don't know if this is OK for you, but if you have the torrent protocol registered to an installed application, simply launching the URL as if it were the path of an executable file (for example by using the Process class) will launch the associated application. See here: http://kb.mozillazine.org/Register_protocol
Try this:
Process p = new Process();
p.StartInfo.FileName = "http://domain/folder/file.torrent";
p.Start();
Or, if you like one-liners:
new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "http://domain/folder/file.torrent"
}
}.Start();
That will call your default browser to download that file and tries to open it. Clicking "Open" you associate program takes control.

killing process java.exe

I launch some batch file from my C# app via Process class. This file started java app. I cant recognize which process called java.exe must be kill to stop this application. Is there some more elegant way than kill them all, hope that i end the right one and dont kill some other important java app? Thanks for any help I really stuck there.
Cheers,
Andrew
EDIT: I missed the batch file part. This answer will only be useful if you can launch the process directly from C#...
When you start the process you can store the process Id so you can get the process back later and kill it.
Process p = new Process();
//set all the process start Info...
p.Start();
someVariableICanGetToLater = p.Id
//Later when you need to kill the process...
Process pToKill = Process.GetProcessById(someVariableICanGetToLater);
pToKill.Kill();
I assume that, since the batch file launches the Java app, knowing the id of the batch file process won't help.
Process.GetProcessesByName("java") gives you a list of all the java.exe instances.
pseudo code . Fill in OS specific calls here:
int cs_pid=getProcessByName("CSharpAppName");
int javaPid[]= getProcessByName("JavaAppName");
bool javaToCSharpRelationship[]=new bool[javaPid.size];
int loopCounter=0;
forEach int in javaPid
begin
if(isChildOf(cs_pid,java_pid))
begin
// OS call or Wrapper to determine relationship between c# pid
// and Java app PID
javaToCSharpRelationShip[loopCounter]=true;
//can call kill here if you like
end
else
javaToCSharpRelationShip[loopCounter]=false;
loopCounter++;
end
At the end of this code you should have an array of PID's which are children of the C# app.
Unless the batch file does lots of things, hard to accomplish using only .NET's Process and ProcessStartInfo classes, another option would be to start directly java.exe and control the arguments and environment variables via ProcessStartInfo's Arguments and EnvironmentVariables properties.
Then you could use Jason's approach to store java.exe process' id in order to kill it later.

Categories