performing two tasks on right click of a file, c#
i have added an item in the context menu of windows explorer using registry. i want that when user right clicks on a file and then clicks on my added context menu item then an application must execute as well as location of that file must be availabe.
My application gets executed but i coul开发者_如何学编程d not get the path of file on which i right clicked
How exactly did you add that context menu item? You need to pass the file name somewhere which is usually done by putting %1
into the command line to be executed.
So if the command you're currently executing is
"C:\Program Files\MyCoolProgram\mcp.exe"
it should be
"C:\Program Files\MyCoolProgram\mcp.exe" %1
Take a look here: A simple C# function to add context menu items in Explorer and notice this part:
AddContextMenuItem(".zip", "ZipStrip",
"Open with &ZipStrip", Application.ExecutablePath + " %1");
This way you'll receive your full file path as first argument on your Main(string args[])
EDIT: If you need to deal with arguments containing white spaces, try this:
AddContextMenuItem(".zip", "ZipStrip",
"Open with &ZipStrip", Application.ExecutablePath + " ""%1""");
This way you argument will be enclosed into double quotes, preserving that white spaces.
The path is passed as a command line arguments. In the entry point, you can access it by defining a string[]
argument:
static void Main(string[] args) {
// the path is passed in the `args` array.
}
It should also be available by calling Environment.GetCommandLineArgs
method.
精彩评论