Reputation: 1284
i'm trying to find the most generic way to understand if a file is being "used" by any application.
this is true also for applications like notepad/onenote/notepad++ (which don't lock the file).
i'm handling the opening of the files through my app (using Process.Start), and i need to delete the file when the user finishes working on it.
what i managed to come up until now is the following:
i'm totally lost about "Notepad++" applications or similar to them. notepad++ doesn't have any lock on the file or folder, and it uses single process. i 'm not sure how to handle this scenario, when doing Process.Start i don't even have a name of the process. (i can check the registry maybe to see who's the default app that opens the file)...
so, any other directions?
thanks!
== EDIT ==
well, currently i've decided about the following logic, unless there's a flaw in it:
that's the idea i think
Upvotes: 1
Views: 320
Reputation: 4404
I am guessing you are trying to do this,
static void ShowFile(string fileToShow) {
using (Process proc = new Process()) {
ProcessStartInfo psi = new ProcessStartInfo(fileToShow);
psi.UseShellExecute = true;
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
}
}
But, maybe you should do this
static void ShowFile(string fileToShow) {
using (Process proc = new Process()) {
ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files (x86)\Notepad++\notepad++.exe", "-multiInst " + fileToShow);
psi.UseShellExecute = false;
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
}
}
And then when your function returns, you can delete your temp file.
I understand that you might not know if the system already has notepad++ installed and its path, etc. In that case you can try to use the path for notepad.exe which comes with windows.
The thing is, there is no perfect solution to what you are looking. To put it in other words, no file viewer/editor is guaranteed to
You can find out the default application registered to open your file, but you don't know if it starts as a single instance or a multi-instance app or they communicate using DDE, etc
Upvotes: 0