Opening application with file C# Winforms - c#

So I'm making a Notepad replica in my spare time just for fun, the core of the project is all done. However, I can't open my application from a file. For example, if you had a .txt file, you double click it and it would open the app (by default its Notepad).
I already know how to set the default application, but the code does not support opening the files yet, it can only open files from the menu inside the program.
How would I go about making it so that my application can be opened by files?

in you void Main(string[] args) method, the args will contain the path to the file that was opened with your application. You can also get them anywhere in the application by calling string[] Environment.GetCommandLineArgs(). Print the argument in a message box to see what arguments have been passed.
Here's some code you might want to use for that:
//In a form
public void MyButton_Clicked(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
MessageBox.Show(string.Join(Environment.NewLine, args));
}

I like your question. Once you provide support for opening type files, and you find that your application is still not working, perhaps this might be useful: you can check under registry section HKEY_CLASSES_ROOT for the extension and action details. It talks about default applications, but it gets into more details.
Here's the page:
Finding the default application for opening a particular file type on Windows

Related

How to raise the event while double click on any file

I want to write/capture the event when I double click on my file located in Windows folder. Assume that we have some txt file located in D:\MyTest\Example1.txt
When I double click on the file, the file should not open whereas it should fire an event/trigger/service to execute some tasks.
I tried with FileSystemWatcher in C#. But, when I double click on the file, the file is getting opened.(It should not).
A simple requirement for your better understanding given below.
When you double click on any txt file in Windows, it should invoke a simple batch file (Example: a.bat) to display the today's date.
Conditions: When you double click on the file, the .bat file should run without opening the actual txt file.
FileSystemWatcher monitor specific path for any file add/remove/modification and not for file execution.
There are different solution based on your needs :
The simplest solution is to register your program for own file extension. by double clicking on file your program got executed and you can just log it and do nothing any more or open the requested file using actual program. for example "Notepad.exe 1.txt"
Write a hook module and load it into explorer's process and watch for file execution but the chief drawback of this solution is third party apps or tricks to open a file that make no sense in your program. for example using CMD.exe or PowerShell.exe to open text file because you monitor specific program.
You can inject your hook module into all executables but it is possible to make performance issues.
The beset solution is to write MiniFilter driver to monitor file system but it need to good system, user mode, c++ and driver development knowledge.
It's possible to implement a virtual file system and control everything over it and again it need to driver development but there is good C# ports for existing libraries, the best one for C# is Dokan .Net
Describe the simplest way to implement :
Write simple program without any form like this :
static void Main(string[] args)
{
if (!args.Any())
return;
foreach (string argument in args)
{
if (Path.GetExtension(argument) != "txt")
continue;
if (Path.GetDirectoryName(argument) == #"D:\MyTest\")
{
// Call your service event here
return;
}
Process.Start("Notepad.exe", argument);
}
}
Register your program for own file extension (in your case .txt) using registry configuration. Here is Microsoft documentation and maybe this topic help you.
Now by double clicking on a .txt file your program got executed and path of the file(s) passed to your program as arguments. program check each file and if you want you can call something or notify your service using different mechanisms like Pipes or Messages or etc.
If the file is not match you want the original application called (in my sample code "nodepad.exe") and the file open normal.

C# : capturing the file and path of a file when right I right click on the file

I am developing a windows application with c#. I need to get the file and folder name when I right click on a file to use for other functions. Right now I have made the registry keys to get my application running.
When I right click on a file (word, ppt, txt, etc) my context menu shows the option I added, Set Perm/ When that is selected my windows application opens up. In a text box on the form, I want to display the full file name and path of the file the user clicked on. Is there a way to do that?
I've added %0 and %1 in the registry to then end of the arguument to launch my app, but it does not work. I've tried h:\temp\myapp.exe %1 and h:\temp\myapp.exe %0. Putting the %1 or %0 in quotes did not change anything.
I am in Windows 8 using Visual Studio 2017. Searching brought me to this question, but it has not answered my problem yet.
if I understand what you're asking,
to have the path passed in the register,
void Main(string[] args)
the Path class has some useful methods to get to the folder and the string of a string
Path.GetFullPath("the path")
sometimes path is args[0](when you drag the file on icon), but in your case you have to iuse args[1].
I hope I understand what you're asking for
Thanks for the help. The question referenced earlier did provide the answer. I was having issues with the form I was using. After working it with for a while I must have really messed it up. I created a new form and used it, using the code provided in that post i referenced.

Open a folder in Windows Explorer and select a file works second time only

Using the code described in this answer of the SO posting "Open folder and select the file", I've created this function:
public static void OpenExplorerAndSelectFile(string filePath)
{
Process.Start(
#"explorer.exe",
string.Format(#"/select, ""{0}""", filePath));
}
This function works well, with one small issue:
Calling the function for the first time for a specific file, Windows Explorer is correctly shown with the folder of the file, but it does not select the file.
Calling the same function again for the same file, it switches back to the already open folder in Windows Explorer and then it selects the file.
E.g. the first call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") opens the folder "C:\MyFolder" in a new Windows Explorer Window. The second call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") actually activates that Window again and selects MyFile.txt.
Doing something similar in e.g. Google Chrome (Going to the download page and showing a previously downloaded file) actually works well right in the first try.
So my conclusion is that Google Chrome seems to do it a bit different than I do.
My question:
Is there a way to debug/trace the Win32/Shell method that Google Chrome calls?
I would then compare them to what I do to see the differences.
Rather than the explorer command line Chrome most probably uses the more flexible SHOpenFolderAndSelectItems Shell API.
This answer contains the required p/invoke/implementation.
Try using the shell function "SHOpenFolderAndSelectItems".

How to open a file using a specified third party program in C#?

so what I'm trying to do is open a file (well, actually two folders, but I figure I'll start with a single file for now) using a third party comparison tool called UltraCompare. I'm working in a C# website project in Visual Studio 2010 (Express edition). I've seen how to open a file using a different program, here: Open a file with Notepad in C#.
Problem is, this only lets you open it using the default program for that file type. But I want to open it in a specified program. For example, a text file should open in UltraCompare, not notepad. Here's the code which does this:
string textBoxContents1 = TextBox1.Text;
Process.Start(textBoxContents1);
The textbox on the webform accepts a string, in which the user types the file's full path (not the most user-friendly design I know, but I'm not sure how to allow them to browse for a folder using a GUI interface in asp.NET). The file is then passed into the Process.Start() method, which opens it using the default program for that file type.
Is there any way to modify this to make it open using UltraCompare??
You can specify the program you want to open the file in:
Process.Start("yourprogram.exe", textBoxContents1);
Update
To open two files in Ultracompare, you'd probably do something like that:
Process.Start("yourprogram.exe", "file1.txt file2.txt");
Keep in mind that the second parameter of Process.Start method are the arguments passed to the program.
I said this is probably going to work because I assumed to be very likely that Ultracompare expects 2 arguments, but this might not be the case.
Quick question: Are you trying to do this for the client machine? Hope not
And I guess it looks into the PATH variable for finding your exe

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