I am wondering if someone could point me in the right direction. You know how for example, in most IDEs, if you open a source file with "open with", it runs the program and opens it up? and then if you open another one, it opens it in a new tab in the same process?
My question is NOT how to add a program to the shell commands, but rather:
How would a C# application "receive" a PDF file for example?
How would the application open the file in the same process when another file is run with it (not having to instances of the program)?
When the second program instance starts up, before loading any interface components, it checks to see if another program instance is already running. If so, it communicates to it in some fashion (program specific: this can be sockets, inter process interrupts, shared memory, etc..) that it should open this new file.
After communicating this to the first instance, the second program instance will just terminate since it's no longer needed.
Your program would have to be able to talk to other instances of itself, and say "hey, I'm already open, what are you trying to do, let me do it for you."
Here is a nicely detailed post that explains the proper implementation:
http://www.iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx
This thread contains a discussion and sample for handling the command line arguments (this is how files are "passed to" your application): http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic62109.aspx
Microsoft programs usually have a ddeexec key in the shell configuration; this will cause the shell to send a DDE command to the already running app, if it exists.
Related
(Sorry for my bad english)
Hi I have a problem.
I need to open the associated files already in my program while it is running.
If we take example visual studio, when I opened the ide and I double-click on a file example test.cs
Do not opening a new application,
But he added a tab with the new file.
And 'possible to do such a thing?
As you've observed, Windows Explorer will try to launch a new instance of your app when you double-click a file associated with your application. You can't change that. What you can do is have the new instance check whether an instance is already running and, if so, tell the running instance to load the file. There are lots of ways to establish this sort of communication. You could listen on a port, periodically check for a file in a particular location, etc. How to do it is up to you.
I want to write a monitor program. It will monitor a special software. When this software open files, it remember the launch history. So I can make my own "Favorite file" or "History" system for some software.
i.e. I use Total Commander frequently. I used tc as a program launcher. Most of my docs or programs opened in TC. But TC does not have a open file history system. So I plan to make one.
When the files opened in TC. TC is the parent process. I think there is a way to write C# code to get all the files opened by TC (It is about message sending and monitor code). TC is written in delphin, It use stand listbox control. C# could solve the problem nice.
But I am new to C#, I have a little Autohotkey and python programming skill. I am learning C# now. Can someone give me some tips to write the code? Core idea is OK, I will handle with the GUI things.
You may not need to write your own. Process monitor from sysinternals/Microsoft can monitor files. And with the filters you can filter by process.
http://technet.microsoft.com/en-us/sysinternals/bb896645
im also doing a child monitoring software for university project.
i found a few solutions for doing this.
Windows HOOK and intercept all the fileread write..etc.(hard & complex)
why not just check MyRecentDocuments folder... :P ( user may change settings which has both pros and cons)
donno if we can use "Process" class and do something.. :P
EDIT----------------
i almost forgot...
when i was implementing the Process Monitoring Component, i found that when a user double clicks a file "dllhost.exe" gets to run. :) i guess u hv a clue of what to do now.. im also still working on it..
I have a question that I believe that is complex. I have an application that I execute under my Windows and it takes a long time to finish. I want to keep it running (normally), however I want to kill the file on disk - but obviously it's not possible because it's locked / in-use. I need a way to disassociate it from the running process to kill it and at the same time keep the file running. Any example of code or tool is very welcome.
Well, workarounds are welcome, for example, if there is a way to spawn it from a process, key the master and migrate the child to kill the app, or any other idea that works is welcome - even the ugly ones. :)
Thanks.
A couple of suggestions (completely stolen) from this questions answers:
You could use the MoveFileEx api function to mark the file for deletion upon next reboot.
You can inject a dll to close the handle yourself:
The typical method is as follows. You've said you want to do this in C# so here goes...
If you don't know which process has the file locked, you'll need to examine each process's handle list, and query each handle to determine if it identifies the locked file. Doing this in C# will likely require P/Invoke or an intermediary C++/CLI to call the native APIs you'll need.
Once you've figured out which process(es) have the file locked, you'll need to safely inject a small native DLL into the process (you can also inject a managed DLL, but this is messier, as you then have to start or attach to the .NET runtime).
That bootstrap DLL then closes the handle using CloseHandle etc.
Essentially: the way to unlock a "locked" file is to inject a DLL into the offending process's address space and close it yourself. You can do this using native or managed code. No matter what, you're going to need a small amount of native code or at least P/Invoke into the same.
Helpful links:
http://www.codeproject.com/KB/threads/winspy.aspx
http://damianblog.com/2008/07/02/net-code-injection/
That is a matter the application you want to kill has to handle. It shouldn't keep files open during a long running process. If the application doesn't close the file, killing it will lead to exception in that application.
Not sure if this will work on every Windows version, but here it is:
Rename process executable "foo.exe" to "foo.old"
Put new "foo.exe" to correct place
Send message to process, so it will execute new "foo.exe" image and terminate himself.
On start, remove "foo.old" file in program directory.
Update: oops, looks like you do not want to put new image, just remove old one. Then MoveFileEx is only "legal" option.
My WPF app can open and edit single documents. I am looking for a tidy approach to allow multiple instances of my WPF app to run but to only allow a given document to be open in one instance of the app. If a user tries to open a document which is already opened in another instance, I need to pop up a dialog to tell them and allow them to switch to the other app instance if required.
Thanks
Dan
One approach is to attempt to take an exclusive lock on the file when you open it. When your other application instance attempts to open the file, an IOException will be raised. You can catch this exception and show a dialog to your user stating that the file is already opened in another application. This scenario should be covered anyway, as the file could be open in a different application that is not yours.
I want to open files, which are doubleclicked in Explorer, in the same instance of my .Net application, e.g. in a new tab. How can I do this?
For now each file starts new instance of my application. Many programs can open files in the same instance, for example, Opera and Notepad++, so there is an easy way for sure.
You may take a look at this post which illustrates a technique that could be used to have a single instance WinForms application.
Might be an easier way to do it but the way I've done it is that if an instance is started with a filename as a parameter then it checks if there are any other instances and if so passes on the filename to that instance and the close itself down.
In case you want to do the same, but in WPF rather than WinForms, the howto is explained here: What is the correct way to create a single-instance application?
Example using TCP-sockets:
http://pieterjan.pro/?a=Projecten_csharp_DrawIt.php
start TCPListener on the form
connect TCPClient in the main of the second instance
Send ActivationArguments through the TCP-connection to the form
Works for multiple files at once as well, and even for multiple files at the first time (when application not started yet)
The most important code blocks are:
The constructor of the MainForm (Hoofdscherm) where the server is started and the port number is written to a file. The first file is opened as well.
The Main-function (Program.cs) where the second, third, ... instance connects to the TcpListener in the first instance and sends the filename through the socket
The source code is available on the button "Broncode"