Click here to Skip to main content
16,021,765 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?

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

            foreach (string arg in Environment.GetCommandLineArgs())
            {
                MessageBox.Show(arg);
            }
        }
Posted
Updated 27-Jul-12 8:08am
v4

The Environment object contains a number of properties that give you things like the path to the exe, the full command line, etc.
 
Share this answer
 
Comments
Edson Rodrigues da Silva 21-Jul-12 14:41pm    
The only way I know to get the name of the file that I click is through the Environment.GetCommandLineArgs () method, I researched other ways to do this and I did not find.
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.
C#
static void Main()
        {
            foreach (string s in Environment.GetCommandLineArgs())
            {
                MessageBox.Show(s);
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }


and

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


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.
 
Share this answer
 
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.

C#
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()
 
Share this answer
 
Comments
Sandeep Mewara 27-Jul-12 23:47pm    
Mark it as solution to be clear for others in future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900