Search code examples
c#filedirectorycommand-line-arguments

Create index.php file in directory


I want to write a program to be added to the right-click menu and when I run the program from the right-click menu to create an index.php file in current directory!

Exmaple:

  • I go to C:\wamp\www

  • Then I right-click somewhere and want to choose create index.php

  • This should create a new file called index.php in the directory I clicked

Desired action:

A new file called index.php wil be created at C:\wamp\www\index.php

More Details:

I want to make it look like "Text Document" in "New":

right-click => new => text document

when we click it windows create "New Text Document.txt" ... now i want when we click on my program windows create "index.php" file!

Registry path that this program will be operate within it:

HKEY_CLASSES_ROOT\Directory\Background\shell\

thanks to all ...


Solution

  • Have a look at this:

    using System;
    using System.IO;
    
    namespace createIndex_php
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = string.Empty;
                try
                {
                    if (File.Exists(args[0])) // right clicked on a file in explorer
                    {
                        path = Path.GetDirectoryName(args[0]);
                    }
                    else
                    {
                        path = args[0];
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format(@"Error while creating ""index.php"": {0}", ex.Message));
                    Console.ReadKey();
                }
    
                if (path != string.Empty)
                {
                    path = Path.Combine(path, "index.php");
                    try
                    {
                        File.Create(path);
                        Console.WriteLine(@"""index.php"" created!");
                        Console.ReadKey();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format(@"Error while creating ""index.php"": {0}", ex.Message));
                        Console.ReadKey();
                    }
                }
            }
        }
    }
    

    This will do the creation of the index.php file.

    Anyway, you have to subscribe your app to the right click menu using regedit and go to following key:

    HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers
    

    Please see the following links for that: