Search code examples
c#wpf

C# WPF Failed to load assembly when trying to associate file extension with my GUI


I am trying to associate opening a file extension (.flw) with my application so that you can open this extension and my application opens and loads info from it So in the MainWindow, I added the following:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    string[] args = Environment.GetCommandLineArgs();

    foreach (string arg in args)
    {
        if (System.IO.Path.GetExtension(arg) == ".flw")
        {
            Console.WriteLine("Path = " + arg);
            SerializationHelper.LoadFlow(arg);
        }
    }
}

The '.flw' file is being open and read correctly, but when I open the application using this extension, one of the application's dependencies, "Framework.DLL" seems to be missing, as if it is being searched for in the same directory as the file.flw.

For example when I double click a 'flw' file that is located on my Desktop, it says:

Could not load file or assembly file 'Desktop\Framework.dll' or one of its dependencies. The system cannot find the file specified.

This specific dependency a C# DLL that I use for my application's styling and I add it as a reference.

It is installed with the application in the same path as the .exe path, but it seems that when I open my application with '.flw' file, it tries to search for this assembly in the same directory as my file

Note that opening the application from its Desktop shortcut doesn't cause this problem.


Solution

  • I was able to solve it by changing the current directory of my application.

    It seems that opening the application from any other file rather than the exe file, the application's current directory is set to the location of this file, so it cannot find the dependencies.

    So in my App.xaml.cs I set the current directory to that of the exe path

    string exePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    Console.WriteLine("exe path = " + exePath);
    System.Environment.CurrentDirectory = exePath;