您的位置:首页 > 编程语言 > C#

C#双击文件只让同一个程序打开文件

2010-04-07 22:05 591 查看
Form1.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace Mydoc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void SaveReg(string p_Filename, string p_FileTypeName)
{
RegistryKey _RegKey = Registry.ClassesRoot.OpenSubKey("", true); //打开注册表

RegistryKey _VRPkey = _RegKey.OpenSubKey(p_FileTypeName);
if (_VRPkey != null) _RegKey.DeleteSubKey(p_FileTypeName, true);
_RegKey.CreateSubKey(p_FileTypeName);
_VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
_VRPkey.SetValue("", "Exec");

_VRPkey = _RegKey.OpenSubKey("Exec", true);
if (_VRPkey != null) _RegKey.DeleteSubKeyTree("Exec"); //如果等于空 就删除注册表DSKJIVR

_RegKey.CreateSubKey("Exec");
_VRPkey = _RegKey.OpenSubKey("Exec", true);
_VRPkey.CreateSubKey("shell");
_VRPkey = _VRPkey.OpenSubKey("shell", true); //写入必须路径
_VRPkey.CreateSubKey("open");
_VRPkey = _VRPkey.OpenSubKey("open", true);
_VRPkey.CreateSubKey("command");
_VRPkey = _VRPkey.OpenSubKey("command", true);
string _PathString = "/"" + p_Filename + "/" /"%1/"";
_VRPkey.SetValue("", _PathString); //写入数据

}
public static void DelReg(string p_FileTypeName)
{
RegistryKey _Regkey = Registry.ClassesRoot.OpenSubKey("", true);

RegistryKey _VRPkey = _Regkey.OpenSubKey(p_FileTypeName);
if (_VRPkey != null) _Regkey.DeleteSubKey(p_FileTypeName, true);
if (_VRPkey != null) _Regkey.DeleteSubKeyTree("Exec");

}

private void Form1_Load(object sender, EventArgs e)
{

SaveReg(Application.ExecutablePath,".task");
string[] argv=Environment.GetCommandLineArgs();
if (argv.GetLength(0) == 2)
{
MessageBox.Show(argv[1]);
}
}
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case 0x004A:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
MessageBox.Show(mystr.lpData);
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;

}
}

Programe.cs代码:

using System.Collections.Generic;
using System.Linq;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Threading;
namespace Mydoc
{
static class Program
{
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}

[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
ref COPYDATASTRUCT lParam // second message parameter
);

[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName,string
lpWindowName);

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool bExist=true;

Mutex MyMutex = new Mutex(true, "ONLYONETASK", out bExist);
if (!bExist)
{
string[] cmds=Environment.GetCommandLineArgs();
if (cmds.Length == 2)
{
int WINDOW_HANDLER = FindWindow(null, @"Form1");
if (WINDOW_HANDLER != 0)
{
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = cmds[1];
cds.cbData = cmds[1].Length*2;
SendMessage(WINDOW_HANDLER, 0x004A, 0, ref cds);

}
}
}
else
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐