簡體   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