C#双击文件只让同一个程序打开文件
来源:互联网 发布:信欣美妆淘宝店假货 编辑:程序博客网 时间:2024/06/06 03:37
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());
}
}
}
}
- C#双击文件只让同一个程序打开文件
- C#双击文件只让同一个程序打开文件
- 问题:双击Excel文件提示文件找不到,只打开程序不同时打开文件,但通过打开对话框却能打开文件。
- 让vs2008与vs2012同时打开同一个项目文件
- 让vs2008与vs2012同时打开同一个项目文件
- 双击打开 .ipynb,双击打开jupyter 文件
- 让自己的软件实现双击打开文件
- C#设置某类文件双击默认打开方式
- C#如何让同一个窗口只能打开一次和一个程序只能同时打开一个
- C#如何让同一个窗口只能打开一次和一个程序只能同时打开一个
- 连续两次打开同一个文件
- fopen多次打开同一个文件
- winform里双击打开文件
- 双击默认文件打开方式
- 自定义文件双击打开事件
- 双击打开运行sh文件
- winform双击文件打开应用程序
- MyEclipse2015双击不能打开文件
- Java.lang.String 类常用方法
- 学习英文之社区,博客及源码
- VC 读注册表获取安装程序
- POJ-2367-Genealogical tree(拓扑排序)
- android 实现APK之间的跳转
- C#双击文件只让同一个程序打开文件
- oracle wm_concat函数,用于列转行,逗号分隔
- 记录一些易忘的属性(translucent,edgesForExtendedLayout,automaticallyAdjustsScrollViewInsets)
- Bone Collector II
- poj 1047 模拟(含大数乘法)循环数
- hdu 5294 Tricks Device (最短路+最大流)
- uvalive5798(树状数组)
- android mk脚本的编写
- VC 读写注册表