资源管理器中的“打开方式”不会启动我的应用程序

本文关键字:启动 应用程序 我的 打开方式 方式 资源管理器 | 更新日期: 2023-09-27 18:36:28

我正在编写一个用于读取和写入我自己的文件类型的 Windows 窗体应用程序。该程序是使用Visual Studio 2015,.NET 4.6制作的。

当我要将我的程序与文件类型相关联时,我遇到了一个问题,通过右键单击文件,单击打开方式,最后我选中了始终使用此程序打开文件扩展名的选项。问题是,当我尝试通过双击文件启动程序时,没有任何反应。程序未显示在屏幕上。

打开任务管理器以查看它是否启动,我注意到它实际上确实启动了一小段时间,但在后台运行,然后关闭。

起初我认为这是我的项目的问题,所以我创建了一个新的 Windows 窗体项目,没有更改单个项目设置,而是将文件类型关联到该项目,但我仍然遇到同样的问题。我也在不同的计算机上尝试了相同的应用程序,但我遇到了同样的问题。

请注意,我实际上可以正常打开该程序,即双击Windows资源管理器中的.exe,也可以通过Visual Studio运行它。

这是我在我创建的测试项目中的程序.cs和 Form1.cs 中的内容,它也无法通过关联启动:

程序.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(args.Length == 0 ? new Form1(string.Empty) : new Form1(args[0]));
    }
}

表格1.cs

public partial class Form1 : Form
{
    public Form1(string path)
    {
        InitializeComponent();
        // Nothing here gets called.            
        if (path != string.Empty)
        {
            MessageBox.Show("File path found!");
        }
        else
        {
            MessageBox.Show("No file path found.");
        }
    }
}

这对我来说是新的,因为我以前已将文件类型与我的程序相关联,并且没有问题。然而,它是在早期版本的Visual Studio上,所以我不确定这是否与它有关。

几天来一直让我

感到困惑,所以我来这里寻求帮助,希望能帮助其他有同样问题的人。

我在Windows 7 x64和Windows 10 x86上都试过了。

我感谢对此的任何想法。

资源管理器中的“打开方式”不会启动我的应用程序

应用程序与文件扩展名的关联(适用于Windows 10):

String App_Exe = "MyApp.exe";
String App_Path = "%localappdata%";
SetAssociation_User("myExt", App_Path + App_Exe, App_Exe);
public static void SetAssociation_User(string Extension, string OpenWith, string ExecutableName){
    try {
        using (RegistryKey User_Classes = Registry.CurrentUser.OpenSubKey("SOFTWARE''Classes''", true))
        using (RegistryKey User_Ext = User_Classes.CreateSubKey("." + Extension))
        using (RegistryKey User_AutoFile = User_Classes.CreateSubKey(Extension + "_auto_file"))
        using (RegistryKey User_AutoFile_Command = User_AutoFile.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command"))
        using (RegistryKey ApplicationAssociationToasts = Registry.CurrentUser.OpenSubKey("Software''Microsoft''Windows''CurrentVersion''ApplicationAssociationToasts''", true))
        using (RegistryKey User_Classes_Applications = User_Classes.CreateSubKey("Applications"))
        using (RegistryKey User_Classes_Applications_Exe = User_Classes_Applications.CreateSubKey(ExecutableName))
        using (RegistryKey User_Application_Command = User_Classes_Applications_Exe.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command"))
        using (RegistryKey User_Explorer = Registry.CurrentUser.CreateSubKey("Software''Microsoft''Windows''CurrentVersion''Explorer''FileExts''." + Extension))
        using (RegistryKey User_Choice = User_Explorer.OpenSubKey("UserChoice")){
              User_Ext.SetValue("", Extension + "_auto_file", RegistryValueKind.String);
              User_Classes.SetValue("", Extension + "_auto_file", RegistryValueKind.String);
              User_Classes.CreateSubKey(Extension + "_auto_file");
              User_AutoFile_Command.SetValue("", "'"" + OpenWith + "'"" + " '"%1'"");
              ApplicationAssociationToasts.SetValue(Extension + "_auto_file_." + Extension, 0);
              ApplicationAssociationToasts.SetValue(@"Applications'" + ExecutableName + "_." + Extension, 0);
              User_Application_Command.SetValue("", "'"" + OpenWith + "'"" + " '"%1'"");
              User_Explorer.CreateSubKey("OpenWithList").SetValue("a", ExecutableName);
              User_Explorer.CreateSubKey("OpenWithProgids").SetValue(Extension + "_auto_file", "0");
              if (User_Choice != null) User_Explorer.DeleteSubKey("UserChoice");
                  User_Explorer.CreateSubKey("UserChoice").SetValue("ProgId", @"Applications'" + ExecutableName);
              }
              SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
        }
        catch (Exception e)
        {
            //Your code here
        }
  }
  [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

你的代码呢,一般它必须按原样工作,但最好通过以下方式更新代码:

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // The first element in the array contains the file name of the executing program. 
        // If the file name is not available, the first element is equal to String.Empty. 
        // The remaining elements contain any additional tokens entered on the command line. 
        var args = Environment.GetCommandLineArgs()[0];
        var path = (args.Count == 1) ? args[0] : args[1];
        if (path == string.Empty)
        {
            MessageBox.Show("No file path found.");
        }
        else
        {
            MessageBox.Show("File path found!");
        }
    }
}

没有尝试代码,因为我现在无法访问 VS。所以可能会有一些代码错误。