none
How to capture FileNotFoundException on missing references DLL file?

    Question

  • 
    
    

    hi,

    I have developed some C# WPF applications which refers to some existing DLL files. Those DLL files are copied to the same folder of the execution file. And I refers the DLL files by add them in the Porject Explorer -> References.

    But my CUSTOM always copy these execution files to other place without the DLL files. The application will be crashed without any imformation or alert, but only log an Application Event in the windows event log. My custom don't know this way, so they always tell me that the application can't run without any information.

    Under the C# Win Form program, I can capture the FileNotFoundException by register the Exception Handler in the Porgrams::Main(), and tell the custom "Missing DLL files".

    But I can't capture this FileNotFoundException (missing DLL files) under the WPF program even I add the Exception Handler at the APP::OnStartUp or APP::APP() function.

    This means the the FileNotFoundException happens before the APP::OnStartUP or the APP::APP().

    Could any one help me how to capture this exception under the WFP program? Thank you very much.

    Best regards.

    
    
    
    
    
    


    Tuesday, March 12, 2019 8:44 AM

All replies

  • 
    
    

    hi,

    I have developed some C# WPF applications which refers to some existing DLL files. Those DLL files are copied to the same folder of the execution file.

    But my CUSTOM always copy these execution files to other place without the DLL files. The application crashs without any imformation or alert, but only log an Application Event in the windows event log. My custom don't know this way, so they always tell me that the application can't run without any information. Under the C# Win Form program, I can capture the FileNotFoundException and tell the custom "Missing DLL files". But I can't capture this exception even I add the Exception Handler at the APP::OnStartUp method. Could any one help me how to capture this exception under the WFP program? Thank you very much. Best regards, (and why I can't enter a new line in this box ....)

    
    
    
    
    
    

    Hi  Charles Wang 329101,

    You can try to check the dll through the DoTestCallDLL(), and use the AppDomain.UnhandledException to catch the error.

            private void Application_Startup(object sender, StartupEventArgs e)
            {
    
    
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += new UnhandledExceptionEventHandler(
                    (senders, args) =>
                    {
                        Exception ex = (Exception)args.ExceptionObject;
                        Console.WriteLine("UnhandledException handler: " + ex.Message);
                    });
    
                DoTestCallDLL();
             }
    
            private void DoTestCallDLL()
            {
                 
            }
    AppDomain.UnhandledException Event

    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 5:17 AM
    Moderator
  • hi, Yong,

    Thank you very much.

    But your code can't capture the Exception, because the Exception is triggered before the Exception handler registerred in APP::APP() or APP::StartUp(...).

    I refers the DLL files by add them in the Porject Explorer -> References.

    And what I want is to give a message to my custom when they run the execution file without the DLL files, not only flashing crashed, by whatever capture the exception or anyother way.

    You know the CUSTOM is CUSTOM. We can't tell them to looking for the Window Event Log to find out the problem.

    Thank you again.

    Wednesday, March 13, 2019 5:38 AM
  • Hi   Charles Wang 329101,

    >>But your code can't capture the Exception, because the Exception is triggered before the Exception handler registerred in APP::APP() or APP::StartUp(...).

    For this case, I suggest you can package your program, the installation software will automatically install all the files to the specified directory. Avoid users copying themselves.

    Deploying a WPF Application (WPF)


    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 8:33 AM
    Moderator
  • hi,

    Thank you very much.

    You are right, and this is the normal way to do.

    But ... I can't define my custom's activity, especially the custom are fimilar with the computer works.

    So I am just interested if there a way to capture this Exception under WPF, like under the WinForm program.

    If there is not a way to do this, I will build another simple execution file which checks the DLL file, .Net Framework version, and then call the main application.

    Best regards,

    Charles Wang

    Thursday, March 14, 2019 7:48 AM
  • Hi   Charles Wang 329101,

    >>So I am just interested if there a way to capture this Exception under WPF, like under the WinForm program.

    WPF also have a Main function, you can define a new EntryPoint for your WPF application.

    In the main function, you can check the dll file.

    Step 1:  Define a Entry point class 

      //<summary>
        //Entry point class  
        //</summary>
        public static class EntryPoint
        {
            [STAThread]
            public static void Main(string[] args)
            {
                Console.WriteLine("Main");
                MessageBox.Show("Main");
                //check the dll file.
                WpfApp_temp2.App app = new App();
                app.InitializeComponent();
                app.Run();
            }
        }
    Step 2 : Application -> Startup object:






    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 5:49 AM
    Moderator
  • hi,

    Thank you very much again.

    But it still doesn't work.

    My code:

        public static class EntryPoint
        {
            [STAThread]
            public static void Main(string[] args)
            {
                if (!System.IO.File.Exists("XXXXXX.dll"))
                {
                    MessageBox.Show("Can't find the DLL files.");
                    Environment.Exit(0);
                }
                else
                {
                    App app = new App();
                    //app.InitializeComponent();
                    app.Run();
                }
            }
        }

    It still crashed at the beginning.

    But if i comment the App declare and run, it works.

        public static class EntryPoint
        {
            [STAThread]
            public static void Main(string[] args)
            {
                if (!System.IO.File.Exists("XXXXXX.dll"))
                {
                    MessageBox.Show("Can't find the DLL files.");
                    Environment.Exit(0);
                }
                else
                {
                    //App app = new App();
                    //app.InitializeComponent();
                    //app.Run();
                }
            }
        }

    ...

    Best regards,

    Charles Wang

    22 hours 19 minutes ago
  • Hi Charles Wang,    

    >>If there is not a way to do this, I will build another simple execution file which checks the DLL file, .Net Framework version, and then call the main application.

    Unfortunately, your question is really weird, I don't have a suggestion right now. you can try your own solution.

    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    20 hours 54 minutes ago
    Moderator
  • hi,

    Thank you again for all your response.

    Best regards,

    Charles Wang

    19 hours 55 minutes ago