通过打开处理打开程序? [英] Handle program being opened by open with?

查看:102
本文介绍了通过打开处理打开程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C#Windows使用>打开上下文菜单行为

我该怎么做?就像如果我右键单击一个文件并单击打开方式,那么我的程序将如何对该文件进行填充:/。

How do I do this? Like if I right click on a file and click open with, then my program how do I do stuff to that file :/.

推荐答案

我使用以下代码将第一个参数(包含文件名的参数)传递给gui应用程序:

I use the following code to pass the first argument (the one that contains the file name) to my gui application:

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]));
        }
    }

我测试是否有参数。如果不是这样,并且用户在没有任何程序的情况下启动您的程序,那么您可能会在尝试使用该程序的任何代码中得到异常。

I test to see if there is an argument. If not and a user starts your program without one then you may get an exception in any code that tries to use it.

这是我的Form1中的一段代码传入文件:

This is a snippet from my Form1 that deals with the incoming file:

public Form1(string path) {
    InitializeComponent();

    if (path != string.Empty && Path.GetExtension(path).ToLower() != ".bgl") {
        //Do whatever
    } else {
        MessageBox.Show("Dropped File is not Bgl File","File Type Error",      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        path = string.Empty;
    }
    //.......
}

您将看到我正在检查发送的扩展名-我的应用仅适用于一种扩展名类型-.bgl-因此,如果用户尝试打开其他文件扩展名,则我将其停止。在这种情况下,我正在处理删除的文件。此代码还将允许用户将文件拖到我的可执行文件(或相关图标)上,并且程序将与该文件一起执行

You will see that I am checking the extension sent in - my app only works with one extension type - .bgl - so if the user tries to open some other file extension then I stop them. In this case I am dealing with a dropped file. This code will also allow a user to drag a file over my executable (or related icon) and the program will execute wit hthat file

您也可以考虑创建文件关联如果您尚未在文件扩展名和程序之间进行选择。结合以上所述,将允许用户双击您的文件并使您的应用程序打开它。

You might also consider creating a file association between your file extension and your program if you have not already. This in conjunction with the above will allow the user to double click on your file and have your application open it.

这篇关于通过打开处理打开程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆