繁体   English   中英

C#Winforms应用程序从命令提示符和调试器中打开,但不能从文件资源管理器中打开

[英]C# Winforms application opens from Command Prompt and in debugger, but not from File Explorer

我对C#Winforms应用程序进行了编程,使其在文件浏览器中单击文件时打开(通过“使用...打开”命令)。 但是,该应用程序没有打开,只是崩溃而没有任何错误消息。 在任务管理器中,可以看到该应用在消失之前短暂运行。

从测试应用程序中,我能够发现File Explorer将文件路径作为第一个参数发送。 从调试器或命令提示符启动应用程序都可以(即使使用文件路径作为参数)也可以。

这是代码:

public static void Main(string[] args) {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        MainForm form = new MainForm();

        if (args.Length > 0) {
            if (System.IO.File.Exists(args[0])) {
                form.OpenFile_(args[0]);
            }
        }

        try {
            Application.Run(form);
        } catch (Exception e) {
            form = new MainForm();
            form.SetStatus("Something went wrong opening the file.");
            Application.Run(form);
        }
    }

即使使用try / catch块,该应用程序也无法从文件资源管理器启动。 它可以在命令提示符或调试器中正常运行。

确实很简单:

代替:

string SettingsFilePath = "settings.txt";

采用:

string SettingsFilePath = Application.StartupPath + "\\settings.txt";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM