文件关联

using Microsoft.Win32;

static void Main(string[] args)
{
    if (args.Length == 0)  //判断启动程序是否带参数
    {
        string FileExt = ".test";
        string FileType = "Test File";
        string MIMEType = "text/plain";
        string ExeApp = Application.ExecutablePath + " %1";

        RegistryKey RegKey;
        RegKey = Registry.ClassesRoot;
        RegKey = RegKey.CreateSubKey(FileExt);  //创建.test项

        RegKey.SetValue("", FileType); //设置默认 
        RegKey.SetValue("Content Type", MIMEType);

        RegKey = RegKey.CreateSubKey("shell\\open\\command"); //在.test下创建执行程序的键值分支
        RegKey.SetValue("", ExeApp);
        RegKey.Close();

        return;
    }

    strFile = args[0]; //将参数指定的文件名赋给变量
    Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
    this.richTextBox1.LoadFile(strFile, RichTextBoxStreamType.PlainText);
}
Contributors: FHL