c# open file association at runtime - c#

(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.

Related

How to copy files from remote windows machines to another windows machine using C#?

I have a folder in which log files are created continuously through a file simulator. On completion of creation of each file, a background windows service which is a FolderWatcher, should copy each file to a central windows server.
I have used File.Copy() to transfer the files from one machine to another. The issue is my background windows service is not able to know whether the file to be copied is still in use.
Is there any way to know that the file is still in use? I know if we try to open the file which is in use, it will throw exception.
Catch the exception and put the file in a list of file to 'try again' on at a later time.
I would check if the file is in use first, then if it is, add it to a queue that is periodically checked and move the file when its free.
See this stackoverflow question to determine how to see if a file is already open.

Opening two files in same application instance

So I have an application that has a special file type .rxml associated to it.
If the application is already running and I open one of these files, it opens a new instance of the application. Is there a way to make the file open in the same application instance?
Yes, roughly-speaking, a new instance of the application will be launched, detect the existing instance and pass information to it, e.g. the path to the new file to be opened. You have a few options for this task, including WCF and Remoting.

How to open associated files in the same instance of an application

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"

How do programs handle file "opened with..." them?

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.

How to open files from explorer into different tabs

How to open files from explorer into different tabs. I can associate an open with menu with the file type, now when I already have the program working, how to open the new file into another tab, instead of new program.
How to find the already running process exactly, not with the name and send the filename to it.
Let me make myself clear: I want my app to be single instance, so that when user select 10 text files and press enter key, my application will open all the 10 text files into 10 tabs, instead of creating 10 processes. How to do that? How to communicate between various instances of the same process.
EDIT SOLVED: Implemented the functionality using WM_COPYDATA in C# and the SingleApplication class from codeproject.
I am not quite sure what you mean in this question. Are you trying to open Windows Explorer windows into one window with tabs? If that is the case, then I recommend you look into QT TabBar, which extends Windows Explorer to allow for such behavior.
Or perhaps you are trying to have a link open to a new tab in a web browser. If that is the case, this behavior is defined by the web browser itself. For Internet Explorer 7, you can set this behavior under Tools > Internet Options.
In the General tab, click the Settings button next to the "Tabs" section. You will want to set the "Open links from other programs in:" option to open a new tab.
Keep in mind that this behavior is defined by each user, and you can't ever make any guarantees that they will have the same browser settings as you do.
After reading your comments, I think I understand a bit better. It sounds like you want your application to only allow one instance at a time. Since you tagged this post C#, I will assume that is what you are writing your program in.
Codeproject.com has a great tutorial on how to make your program only allow a single instance.
Here is a snippet of code from their site:
static void Main()
{
if(SingleInstance.SingleApplication.Run() == false)
{
return;
}
//Write your program logic here
}
You would want to write code just before the return statement to have the existing instance open the file in a new tab.
If you are able to provide detailed information about what your program is doing, we might be able to help you with some of the specifics.

Categories