如何知道调用可执行文件的文件名? [英] How to know the name of the file that called the executable?

查看:83
本文介绍了如何知道调用可执行文件的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#中的序列化技术创建了一个具有自己扩展名的文件.我将图标和程序的可执行文件合并到该文件中,因此,当我双击该文件时,它将称为我的可执行文件.一切正常,但是当我双击创建的文件时,我希望它出现在程序中,为此,我需要知道调用可执行文件的文件名.

我研究并在下面的此链接中找到了类似的内容,
http://www.csharpcorner.com/UploadFile/mahesh/CmdLineArgs03212006232449PM/
CmdLineArgs.aspx

我使用下面的代码,该代码位于表单的load事件中,因为我没有在应用程序中使用Main方法,该方法应显示正在调用可执行文件的文件的名称.但是它显示的唯一字符串是我的可执行文件的目录和名称.
缺少什么,因此我可以获得调用可执行文件的文件的名称?还有另一种方法吗?我必须输入注册密钥才能将文件名传递给可执行文件吗?

I created a file with my own extension using the technique of serialization in C #. I incorporated to this file an icon and the executable file of my program, so when I give a double click on the file it calls my executable. Everything works perfectly, but when I give a double click on the file that I created I want it to appear in my program and for that I need to know the name of the file that is calling the executable.

I researched and found something like it on this link below,
http://www.csharpcorner.com/UploadFile/mahesh/CmdLineArgs03212006232449PM/
CmdLineArgs.aspx

I use the code below, which is within the form''s load event, because I do not use the Main method in my application, which should display the name of the file that is calling the executable. But the only string it displays is the directory and the name of my executable.
What is missing so I can get the name of the file that called the executable? There is another way to do this? I would have to enter a registration key to passing the filename to the executable?

private void frmArquivoPersonalizado_Load(object sender, EventArgs e)
        {
            InicializarControles();
            oPrincipal.Inicializar();

            foreach (string arg in Environment.GetCommandLineArgs())
            {
                MessageBox.Show(arg);
            }
        }

推荐答案

环境对象包含许多属性,这些属性为您提供诸如exe的路径,完整的命令行等内容.
The Environment object contains a number of properties that give you things like the path to the exe, the full command line, etc.


从您的描述看来,您已经创建了一个类似于图像查看器的程序以及一个类似于jpg图像的数据文件.

类比-您的程序:您的数据文件::图像查看器:Jpg文件.

现在就像当我们用dbl单击jpg文件时,打开的图像查看器一样,您已将数据文件与程序相关联,而现在在您的程序中,您将不知道在调用该程序的当前实例时双击了哪个文件.

如果您使用的是主电源,则可以轻松完成此操作,但是您提到您没有使用它.
理想情况下,您的代码应该可以工作,但是我认为以某种方式不能通过命令行传递名称.

我尝试在同一应用程序的主体以及窗体加载中使用提到的代码.
From your description is seems that you have created a program similar to a image viewer and a data file similar to an jpg image.

Analogy- your Program: your datafile :: Image viewer: Jpg file.

now just like when we dbl click an jpg file is opens with image viewer you have associated your data file with your program and now in your program you wan''t to know which file was double clicked when this current instance of program was called.

you could have easily done it if you were using a main, but you mentioned that you are not using it.
Ideally your code should work, but I think somehow the name is not passed with the command line.

I tried using the mentioned code in the main as well as on form load of the same application.
static void Main()
        {
            foreach (string s in Environment.GetCommandLineArgs())
            {
                MessageBox.Show(s);
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }







and

private void Form1_Load(object sender, EventArgs e)
       {
          // System.Threading.Thread.Sleep(2000);
           foreach (string s in Environment.GetCommandLineArgs())
           {
               MessageBox.Show(s);
           }
       }



并且结果如预期的那样,但是,我注意到有时第二个参数最初是缺少的,但后来却出现了.这可能是我的设置出现问题,但是一切都按预期进行.

一种选择是将您的代码移至main,这实际上会破坏您的表单.



and the results were as expected, however, I noticed that sometimes the second argument was missing initially but appeared later. This could be an issue with my setup, but things are working as expected.

One option would be, to shift your code to main, which actually lodes your form.


我发现了问题,我忘记了在LOCAL_MACHINE创建注册表项,而只是创建了项在CLASSES_ROOT中,现在静态void Main(字符串[] args)和Environment.GetCommandLineArgs()方法都返回我单击的文件的名称.我在下面放置了我需要的所有密钥.

I found the problem, I was forgetting to create a registry key at LOCAL_MACHINE, I was only creating keys in CLASSES_ROOT, now both the static void Main (string [] args) and Environment.GetCommandLineArgs() method return the name of the file that I clicked. Below I put all the keys that I used in case someone need.

private void ClassesRoot()
{
    //Cria uma subkey com o nome da extensão do aplicativo
    Registry.ClassesRoot.CreateSubKey(extensao);
    Registry.SetValue(@"HKEY_CLASSES_ROOT\" + extensao, "", "ArquivoPersonalizado",
        RegistryValueKind.String);

    //Cria uma subkey com o nome do aplicativo
    Registry.ClassesRoot.CreateSubKey("ArquivoPersonalizado");
    Registry.SetValue(@"HKEY_CLASSES_ROOT\ArquivoPersonalizado", "", "ArquivoPersonalizado",
        RegistryValueKind.String);

    //Cria uma subkey com o id do aplicativo
    Registry.ClassesRoot.CreateSubKey(@"ArquivoPersonalizado\CLSID");
    Registry.SetValue(@"HKEY_CLASSES_ROOT\ArquivoPersonalizado\CLSID", "",
        "{EDSON" + extensao.Substring(1) + "-0000-0000-0000-000000000001}", RegistryValueKind.String);

    //Cria a subkey que configura o icone do aplicativo
    Registry.ClassesRoot.CreateSubKey(@"ArquivoPersonalizado\DefaultIcon");
    Registry.SetValue(@"HKEY_CLASSES_ROOT\ArquivoPersonalizado\DefaultIcon", "",
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
        @"\Meus Documentos\Icones\Icones de Programas\AAS.ico", RegistryValueKind.String);

    //Cria a subkey que abre o aplicativo
    Registry.ClassesRoot.CreateSubKey(@"ArquivoPersonalizado\Shell\Open\Command");
    Registry.SetValue(@"HKEY_CLASSES_ROOT\ArquivoPersonalizado\Shell\Open\Command", "",
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
        @"\C#\Arquivo.Personalizado\Arquivo.Personalizado\bin\Release\Arquivo.Personalizado.exe ""%1""",
        RegistryValueKind.String);
}//ClassesRoot()

private void CurrentUser()
{
    Registry.CurrentUser.CreateSubKey(@"Software\Industrias Ederson");
    Registry.CurrentUser.CreateSubKey(@"Software\Industrias Ederson\ArquivoPersonalizado");
    Registry.CurrentUser.CreateSubKey(@"Software\Industrias Ederson\ArquivoPersonalizado\1.0.0.0");
    Registry.SetValue(@"HKEY_CURRENT_USER\Software\Industrias Ederson\ArquivoPersonalizado\1.0.0.0", "",
      "{EDSON" + extensao.Substring(1) + "-0000-0000-0000-000000000001}", RegistryValueKind.String);
}//CurrentUser()

private void LocalMachine()
{
    Registry.LocalMachine.CreateSubKey(@"Software\Classes\" + extensao);
    Registry.LocalMachine.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\" + extensao,
        "ArquivoPersonalizado", RegistryValueKind.String);

    //Cria uma subkey com o nome do aplicativo
    Registry.LocalMachine.CreateSubKey(@"Software\Classes\ArquivoPersonalizado");
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\ArquivoPersonalizado", "",
        "ArquivoPersonalizado", RegistryValueKind.String);

    //Cria uma subkey com o id do aplicativo
    Registry.LocalMachine.CreateSubKey(@"Software\Classes\ArquivoPersonalizado\CLSID");
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\ArquivoPersonalizado\CLSID", "",
        "{EDSON" + extensao.Substring(1) + "-0000-0000-0000-000000000001}", RegistryValueKind.String);

    //Cria a subkey que configura o icone do aplicativo
    Registry.LocalMachine.CreateSubKey(@"Software\Classes\ArquivoPersonalizado\DefaultIcon");
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\ArquivoPersonalizado\DefaultIcon", "",
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
        @"\Meus Documentos\Icones\Icones de Programas\AAS.ico", RegistryValueKind.String);

    //Cria a subkey que abre o aplicativo
    Registry.LocalMachine.CreateSubKey(@"Software\Classes\ArquivoPersonalizado\Shell\Open\Command");
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\ArquivoPersonalizado\Shell\Open\Command", "",
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
        @"\C#\Arquivo.Personalizado\Arquivo.Personalizado\bin\Release\Arquivo.Personalizado.exe ""%1""",
        RegistryValueKind.String);

    Registry.LocalMachine.CreateSubKey(@"Software\Industrias Ederson");
    Registry.LocalMachine.CreateSubKey(@"Software\Industrias Ederson\ArquivoPersonalizado");
    Registry.LocalMachine.CreateSubKey(@"Software\Industrias Ederson\ArquivoPersonalizado\1.0.0.0");
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Industrias Ederson\ArquivoPersonalizado\1.0.0.0", "",
      "{EDSON" + extensao.Substring(1) + "-0000-0000-0000-000000000001}", RegistryValueKind.String);
}//LocalMachine()


这篇关于如何知道调用可执行文件的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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