1

I have a Program that opens and saves files but only from the GUI. I saw a post about it but he needed something different so here I am asking for help.

I want to be able to open files from the desktop so that the data inside the files will load into the program.

I managed to add the file extension but when I double click the file from the desktop completely different window opens up (Activision Window that I programmed).

I use WPF with C# but I run with VB.

Can anyone tell me what am I doing wrong? Is there an event that handles opening files?

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
Dor Peretz
  • 11
  • 5
  • 1
    You will need to bind the extension to an application _(open with...)_. Then you should use the arguments passed to the application to determine if the application is launched with a parameter. `void App_Startup(object sender, StartupEventArgs e) { }` check the `StartupEventArgs` – Jeroen van Langen Oct 14 '19 at 19:36
  • Show your code. – Trevor Oct 14 '19 at 19:50
  • When you say you "managed to add the file extension" do you mean you managed to get it associated with your program? And then when you double click the file with that extension it does open your program, but the wrong window? – Keith Stein Oct 14 '19 at 22:14
  • Yes, when i duble click on the file its open the program, but it open as if the program never opened before, all of "my.setting" data is not there. – Dor Peretz Oct 16 '19 at 06:04

3 Answers3

2

What it seems is missing is you actually loading the data from the file. When you open a program by double-clicking an associated file, Windows passes the path to that file as a parameter.

In WPF, these parameters are accessible in the Application.StartUp event. StartupEventArgs.Args is a string array containing the all the parameters passed. When opening the program from a file e.Args[0] should be the full name of the file that was opened.

In your startup event, you should be setting up your program to open the way you want based on the file.

You can change Application.StartupUri to pick which window opens first.

Alternatively, you can also remove StartupUri from the App.xaml file and create an instance of your window yourself (using the normal Window.ShowDialog method). This gives you the advantage of being able to access the window durring the Startup event and change any properties based on the file/parameter.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
0

This isn't something you do in your program. You need to tell windows to launch your program when someone double clicks a file.

  1. Open Control Panel and navigate to Programs.
  2. Select Default Programs and Associate a file type or protocol with a specific program.
  3. Find the file type that you want to change on the left and highlight it.
  4. Select Change program in the top right.
  5. Select the program from the new window that appears and click OK.

https://www.techjunkie.com/associate-file-types-programs/

Windows will then start your program and pass it the path to the file as a command line argument. See this question for how to do that: How to start WPF based on Arguments

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
0

To get the opened file name from the Windows, use Environment.GetCommandLineArgs method. The Environment.GetCommandLineArgs method returns a string array containing the command-line arguments for the current process. The first element is the executable file name.

To read file content, you could use System.IO.File.ReadAllText method.

Try this code:

string FilePath = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).ToArray());
if (!string.IsNullOrEmpty(FilePath))
{
   string FileContent = System.IO.File.ReadAllText(FilePath);
   Console.WriteLine(FileContent);
}

In WinForm, you could paste this code inside the form_Load event.

MB_18
  • 1,620
  • 23
  • 37