2017-02-16 54 views
2

我有相关的我的程序使用此方法:C#总是0参数

public static void CreateFileAssociation(string extension, string key, string description, string path) 
{ 
    RegistryKey classes = Registry.ClassesRoot; 
    RegistryKey extensionKey = classes.CreateSubKey(extension); 
    extensionKey.SetValue(null, key); 

    RegistryKey typeKey = classes.CreateSubKey(key); 
    typeKey.SetValue(null, description); 

    RegistryKey shellKey = typeKey.CreateSubKey("shell"); 
    RegistryKey shellOpenKey = shellKey.CreateSubKey("open"); 
    RegistryKey shellOpenCommandKey = shellOpenKey.CreateSubKey("command"); 
    shellOpenCommandKey.SetValue(null, path); 
} 

Program.cs

static void Main(string[] args) 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1(args.TryGet(0))); 
} 

而且我Form1.cs

string filenameArg = ""; 

public Form1(string arg) 
{ 
    InitializeComponent(); 

    filenameArg = arg; 
} 

当我打开我的相关文件(扩展名),我的程序以无参数启动

不知道这是什么问题。它出什么问题了 ?

+0

您忘了向我们展示最重要的部分:当您将它写入注册表时,'path'里面有什么? –

+0

我的应用程序是外部的,意味着它可以从任何地方运行,所以我总是在启动时将它复制到appdata \ local \ myProgram 所以路径是:... appdata \ local \ myProgram \ prog.exe – alix54

+0

您在program.cs中显示string [] args; args.TryGet(0); string []在string []中没有定义TryGet方法?我正在用c#6.0来检查这个问题 –

回答

4

你可能错过了%1在你的指令值:

HKEY_CLASSES_ROOT\...\shell\open\command = ...appdata\local\myProgram\prog.exe "%1" 

%1是由您选择的文件的路径所取代。把它放在双引号中,以便带有空格的路径也作为单个参数传递。

+0

哦,谢谢你,忘记了'%1'在我的路径中 – alix54