using GameBackup3H3.DbOp; using Microsoft.Win32; using MyPage; using ryCommon; using ryCommonDb; using ryControls; using 开发辅助工具.Manager.Site; 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 static System.Net.WebRequestMethods; using System.Xml.Linq; using System.Security.Principal; using System.Runtime.InteropServices; namespace 开发辅助工具.Manager { public partial class FrmSetting : Form { public FrmSetting() { InitializeComponent(); OlvSiteName.AspectGetter = delegate (object x) { return ((SiteInfo)x).Name; }; OlvSitePath.AspectGetter = delegate (object x) { return ((SiteInfo)x).LocalPath; }; OlvSiteFTP.AspectGetter = delegate (object x) { return ((SiteInfo)x).FtpName; }; } private void BtnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; } public bool IsAdministrator() { WindowsIdentity current = WindowsIdentity.GetCurrent(); WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); } [DllImport("shell32.dll")] public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); private void BtnOK_Click(object sender, EventArgs e) { if(!IsAdministrator()) { MessageBox.Show("修改文件关联需要以管理员身份启动软件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (ChkAss_File.Checked) { SetFileAssociation("*","", "ryUSQ", "用睿元编辑器打开", Application.ExecutablePath, "\"%1\""); } else { DelFileAssociation("*", "ryUSQ"); } for (int i = 0; i < checkedListBox1.Items.Count; i++) { var item = checkedListBox1.Items[i].ToString(); var check=checkedListBox1.GetItemChecked(i); if (check) { SetFileAssociation("ryUSQ_" + item,item, "open", "", Application.ExecutablePath, "\"%1\""); SetDefFileAssociation("." + item, "ryUSQ_" + item); } else { try { var ExtName = "." + item; RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32); RegistryKey def_key = LRoot.OpenSubKey(ExtName, true); var bak_value = def_key.GetValue("bak_value"); if(bak_value!=null) { def_key.SetValue("", bak_value, RegistryValueKind.String); def_key.DeleteValue("bak_value"); } def_key.Close(); RegistryKey file_key = LRoot.OpenSubKey("ryUSQ_" + item, true); if(file_key!=null) { file_key.Close(); LRoot.DeleteSubKeyTree("ryUSQ_" + item); } LRoot.Close(); } catch { } } } SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); DialogResult = DialogResult.OK; } /// /// 设置文件关联 /// /// public bool SetDefFileAssociation(string ExtName, string defName) { try { RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32); RegistryKey def_key = LRoot.OpenSubKey(ExtName, true); if (def_key == null) { def_key = LRoot.CreateSubKey(ExtName); } if (!def_key.GetValue("", "").ToString().StartsWith("ryUSQ_")) { def_key.SetValue("bak_value", def_key.GetValue("", ""), RegistryValueKind.String); } def_key.SetValue("", defName, RegistryValueKind.String); def_key.Close(); LRoot.Close(); return true; } catch { return false; } } /// /// 设置文件关联 /// /// public bool SetFileAssociation(string ExtName,string true_ext, string sName, string AssDes, string filePath,string param, bool defOpen = false) { try { var _path = filePath.Replace("/", "\\"); RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot,RegistryView.Registry32); if (System.IO.File.Exists(Application.StartupPath + "\\SysDb\\ass_ico\\" + true_ext + ".ico")) { RegistryKey Icon_REG = LRoot.OpenSubKey(ExtName + @"\DefaultIcon", true); if (Icon_REG == null) { Icon_REG = LRoot.CreateSubKey(ExtName + @"\DefaultIcon"); } Icon_REG.SetValue("", Application.StartupPath + "\\SysDb\\ass_ico\\" + true_ext + ".ico", RegistryValueKind.String); Icon_REG.Close(); } // RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell\"+sName, true); if (softwareRun == null) { softwareRun = LRoot.CreateSubKey(ExtName + @"\shell\" + sName); } softwareRun.SetValue("", AssDes, RegistryValueKind.String); var Command= softwareRun.OpenSubKey("command",true); if (Command == null) { Command = softwareRun.CreateSubKey("command"); } Command.SetValue("","\""+ _path + "\" "+param); Command.Close(); softwareRun.Close(); if (defOpen) { RegistryKey def_key = LRoot.OpenSubKey(ExtName, true); if (def_key == null) { def_key = LRoot.CreateSubKey(ExtName); } def_key.SetValue("", sName, RegistryValueKind.String); def_key.Close(); } LRoot.Close(); return true; } catch { return false; } } /// /// 判断文件关联是否存在 /// /// /// /// public bool GetFileAssociation(string ExtName, string sName) { try { var result = false; RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32); RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell\"+sName, false); if (softwareRun != null) { softwareRun.Close(); result = true; } LRoot.Close(); return result; } catch { return false; } } /// /// 判断文件关联是否存在 /// /// /// /// public bool GetFileAssociation(string ExtName) { try { var result = false; RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32); RegistryKey softwareRun = LRoot.OpenSubKey(ExtName, false); if (softwareRun != null) { softwareRun.Close(); result = true; } LRoot.Close(); return result; } catch { return false; } } /// /// 删除文件关联 /// /// /// /// public bool DelFileAssociation(string ExtName, string sName) { try { RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32); RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell", false); if(softwareRun!=null) { var shell_name = softwareRun.OpenSubKey(sName,false); if(shell_name!=null) { shell_name.Close(); softwareRun.DeleteSubKeyTree(sName); } softwareRun.Close(); } LRoot.Close(); return true; } catch { return false; } } private void FrmSetting_Load(object sender, EventArgs e) { ChkAss_File.Checked = GetFileAssociation("*", "ryUSQ"); for (int i = 0; i < checkedListBox1.Items.Count; i++) { var item = checkedListBox1.Items[i].ToString(); if(GetFileAssociation("ryUSQ_"+item)) { checkedListBox1.SetItemChecked(i, true); } } LoadSite(); } private void LoadSite() { List list = new List(); LvSite.ClearObjects(); IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType); if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1) { var ds = db.ReadData("select * from Site"); if(ds.HaveData()) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { var row = ds.GetRow(i); var ftp_ds = db.ReadData("Ftp", row["ftpId"].ToString()); var ftpname = ""; if (ftp_ds.HaveData()) { ftpname = ftp_ds.GetRow(0)["name"].ToString(); } list.Add(new SiteInfo() { Id = row["id"].ToInt(), Name = row["name"].ToString(), LocalPath = row["localPath"].ToString(), FtpId = row["ftpId"].ToInt(), FtpName= ftpname }); } } ds.Dispose(); } db.Free(); LvSite.AddObjects(list); } private void 添加站点ToolStripMenuItem_Click(object sender, EventArgs e) { FrmAddSite frm = new FrmAddSite(); if(frm.ShowDialog()==DialogResult.OK) { LoadSite(); } frm.Dispose(); } private void 修改站点ToolStripMenuItem_Click(object sender, EventArgs e) { if (LvSite.SelectedObject == null) { return; } var item = (SiteInfo)LvSite.SelectedObject; var selectId = item.Id; FrmAddSite frm = new FrmAddSite() { Icon = Icon, isAdd = 0 }; frm.GetInfo(selectId.ToString()); if (frm.ShowDialog() == DialogResult.OK) { LoadSite(); } frm.Dispose(); } private void 删除站点ToolStripMenuItem_Click(object sender, EventArgs e) { if (LvSite.SelectedObject == null) { MessageBox.Show("请先选择要删除的项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var item = (SiteInfo)LvSite.SelectedObject; var selectId = item.Id; switch (MessageBox.Show("确定要删除该项吗?一旦删除将不可恢复。", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) { case DialogResult.No: return; } //DataProvider mydb = new DataProvider(); IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType); if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1) { db.DelById("Site", selectId.ToString()); LvSite.RemoveObject(item); } } private void BtnSelectAll_Click(object sender, EventArgs e) { for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i,true); } } private void BtnNoSelect_Click(object sender, EventArgs e) { for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, false); } } } public class SiteInfo { public int Id { get; set; } = 0; public string Name { get; set; } = ""; public string LocalPath { get; set; } = ""; /// /// 保存的远程路径 /// public string RemoteFullPath { get; set; } = ""; public string FtpDir { get; set; } = ""; public string FtpName { get; set; } = ""; public int FtpId { get; set; } = 0; } }