using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 文件关联测试项目
{
static class Program
{
const int WM_COPYDATA = 0x004A;
[STAThread]
static void Main(string[] args)
{
try
{
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
MessageBox.Show("dddd");
if (args != null && args.Length > 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args[0]));
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
mutex.ReleaseMutex();
}
else
{
if (args != null && args.Length > 0)
{
int WINDOW_HANDLER = GetHandler();
if (WINDOW_HANDLER != 0)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(args[0]);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = args[0];
cds.cbData = len + 1;
SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd,
int Msg,
int wParam,
ref COPYDATASTRUCT lParam
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
static int GetHandler()
{
return FindWindow(null, "AAAA");
foreach (var item in Process.GetProcesses())
{
if (item.MainModule.FileName == Application.ExecutablePath)
{
return (int)item.MainWindowHandle;
}
}
return 0;
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
}
}
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 文件关联测试项目
{
public partial class Form1 : Form
{
const int WM_COPYDATA = 0x004A;
public Form1()
{
InitializeComponent();
RegisterFileType(".b", null, null, System.Windows.Forms.Application.ExecutablePath);
}
public Form1(string arg)
{
InitializeComponent();
richTextBox1.Text = arg;
}
private void Form1_Load(object sender, EventArgs e)
{
Task task2 = new Task(() =>
{
while (true)
{
if (actQueue.Count > 0)
{
DoUI(actQueue.Dequeue());
}
else
{
Thread.Sleep(1);
}
}
});
task2.Start();
}
public void DoUI(Action cb)
{
this.Invoke(cb);
}
public static bool FileTypeRegistered(string extension)
{
RegistryKey sluKey = Registry.ClassesRoot.OpenSubKey(extension);
if (sluKey != null)
return true;
return false;
}
public static void UnRegistFileType(string extension)
{
if (FileTypeRegistered(extension))
{
Registry.ClassesRoot.DeleteSubKeyTree(extension);
Registry.ClassesRoot.Close();
}
}
private static void RegisterFileType(string typeName,string fileType,string fileContent, string app)
{
string toolPath = app;
string extension = typeName;
fileType = "自定义文件类型";
fileContent = "AAAA";
RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension);
if (registryKey != null && registryKey.OpenSubKey("shell") != null && registryKey.OpenSubKey("shell").OpenSubKey("open") != null &&
registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command") != null)
{
var varSub = registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
var varValue = varSub.GetValue("");
if (Equals(varValue, toolPath + " \"%1\""))
{
return;
}
}
Registry.ClassesRoot.DeleteSubKeyTree(extension, false);
registryKey = Registry.ClassesRoot.CreateSubKey(extension);
registryKey.SetValue("", fileType);
registryKey.SetValue("Content Type", fileContent);
RegistryKey iconKey = registryKey.CreateSubKey("DefaultIcon");
iconKey.SetValue("", Application.StartupPath + "\\1.ico");
iconKey.Close();
registryKey = registryKey.CreateSubKey("shell\\open\\command");
registryKey.SetValue("", toolPath + " \"%1\"");
registryKey.Close();
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
}
[DllImport("shell32.dll")]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
Queue<Action> actQueue = new Queue<Action>();
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
actQueue.Enqueue(() =>
{
Loaded();
});
break;
default:
base.DefWndProc(ref m);
break;
}
}
private void Loaded()
{
this.richTextBox1.Text = mystr.lpData;
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
}
}