![](/img/trans.png)
[英]Get the path from an selected windows explorer folder to C# via context menu
[英]c# retrieve file/folder path from selected file/folder in Context Menu Windows Explorer
我正在嘗試在Windows資源管理器的上下文菜單中為任何文件和任何文件夾實現一個選項。 我已經通過寫regedit來實現這一點。
Using Microsoft.Win32;
...
RegistryKey key;
// Register to any file
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\*\shell\MyProject");
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\*\shell\MyProject\command");
// Register to folder
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\Folder\shell\MyProject");
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\Folder\shell\MyProject\command");
// Default value points to the app
key.SetValue("", Application.StartupPath + @"\MyProject.exe");
key.Close();
該應用程序將按我的意願打開,但是我不知道如何獲取上下文菜單中選擇的文件/文件夾的路徑。 我怎樣才能做到這一點?
將注冊表項的值更改為
key.SetValue("", Application.StartupPath + @"\MyProject.exe %1");
因此%1
被替換為選定的文件/文件夾。 在您的主要方法中,您可以通過以下方式訪問此方法:
static void Main(string[] args)
{
Console.WriteLine("Selected file/folder: {0}", args[0]);
}
不幸的是,這不適用於多選。 放置%2
等是沒有用的。 如果選擇了多個文件或文件夾,則將分別為您的應用程序分別調用它們。
RenéVogt的答案只有這一行:
key.SetValue("", Application.StartupPath + @"\MyProject.exe %1");
應該:
key.SetValue("", Application.StartupPath + @"\MyProject.exe \"%1\"");
如果目錄或文件路徑包含空格, args[]
帶引號的args[]
數組將包含多個字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.