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());
}
}
}
}
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());
}
}
}
}
相关文章推荐
- C#双击文件只让同一个程序打开文件
- c#开发一个“记事本”程序,要求能够实现文件的新建、打开、编辑、保存功能
- C#窗体程序怎么点击按钮打开一个选择文件的对话框
- matlab2017安装后,每次双击.m文件都会自动打开一个matlab程序,解决方案!
- matlab双击.m文件另外打开一个matlab程序的问题
- 请问C#程序中又激发一个exe文件的api?如打开记事本。
- matlab的使用问题(—):每次双击.m文件都会自动打开一个matlab程序
- 窗口作业1 作业: 1.创建一个窗口程序(JFrame),标题栏起名为“浏览器”,有一个菜单条,有“文件”、“编辑”、“查看”3个菜单。“文件”菜单有两个菜单项,一项是“打开”,一项是“保存
- 如何使用java程序打开一个文件?
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- C#_WinForm_打开一个进程、文件等.
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- 在WinCE下使用C#来打开一个外部程序
- 双击无法打开我的电脑、程序、文件
- [C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- 为win7下的无后缀文件设置一个默认打开程序
- [C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器
- 每次双击.dsw文件都打开一个新的vc窗口.