0

i create a very simple photo viewer using c# wpf i just need to open the image directly into my app like default windows photo viewer.

so i need to pass the image file as parameter to my app

i found This Question but it for winforms not wpf.

i hope that you could help me.

sorry for bad English.

Community
  • 1
  • 1
Ahmed Tarek
  • 104
  • 10

1 Answers1

4

To handle file open - triggered for example when double-clicking a file in Windows explorer - simply run the following C# code when the application has been started (e.g. in the main window's "Loaded" event):

var args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
    var fileName = args[1];
    if (File.Exists(fileName))
    {
        var extension = Path.GetExtension(fileName);
        if (extension == ".MyDocumentExtension")
        {
             // TODO: Open file from fileName
        }
    }
}
Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • i have made an small edit . now it works . Thanks alot – Ahmed Tarek Nov 10 '14 at 14:36
  • 2
    @AhmedTarek, this answer is about how to pass file as parameter to your program, which for me is different to your question: *to open the image directly into my app like default windows photo viewer.*. You may want to update your question. – Bolu Nov 10 '14 at 14:40
  • Yes, the question for me means "How to register a file association in Windows" but is actually "How to load file from application arguments" – Rico Suter Nov 10 '14 at 15:32
  • So, you changed it back to the original? – Rico Suter Nov 10 '14 at 15:33