📄 installdesigner.cs
字号:
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace install
{
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.GACManagedAccess;
using System.Xml;
using System.Diagnostics;
public partial class InstallDesigner : Form
{
private static Guid standardDataProviderGuid = new Guid("{0EBAAB6E-CA80-4b4a-8DDF-CBE6BF058C70}");
private static Guid standardDataSourcesGuid = new Guid("{0EBAAB6E-CA80-4b4a-8DDF-CBE6BF058C71}");
private static Guid standardCFDataSourcesGuid = new Guid("{0EBAAB6E-CA80-4b4a-8DDF-CBE6BF058C72}");
private static Guid oledbDataProviderGuid = new Guid("{7F041D59-D76A-44ed-9AA2-FBF6B0548B80}");
private static Guid oledbAltDataProviderGuid = new Guid("{7F041D59-D76A-44ed-9AA2-FBF6B0548B81}");
private static Guid jetDataSourcesGuid = new Guid("{466CE797-67A4-4495-B75C-A3FD282E7FC3}");
private static Guid jetAltDataSourcesGuid = new Guid("{466CE797-67A4-4495-B75C-A3FD282E7FC4}");
private static string[] compactFrameworks = new string[] { "PocketPC", "SmartPhone", "WindowsCE" };
internal bool _remove = false;
private string _regRoot = "8.0";
private System.Reflection.Assembly _assm = null;
private bool _ignoreChecks = true;
private string _assmLocation;
string SQLiteLocation
{
get
{
System.Reflection.Assembly assm = SQLite;
return _assmLocation;
}
}
System.Reflection.Assembly SQLite
{
get
{
if (_assm == null)
{
Environment.CurrentDirectory = Path.GetDirectoryName(typeof(InstallDesigner).Assembly.Location);
try
{
_assmLocation = Path.GetFullPath("..\\System.Data.SQLite.DLL");
_assm = System.Reflection.Assembly.LoadFrom(_assmLocation);
}
catch
{
}
}
OpenFileDialog dlg = new OpenFileDialog();
while (_assm == null)
{
dlg.Multiselect = false;
dlg.InitialDirectory = Environment.CurrentDirectory;
dlg.FileName = "System.Data.SQLite.DLL";
dlg.Filter = "System.Data.SQLite.DLL|System.Data.SQLite.DLL";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
_assmLocation = dlg.FileName;
_assm = System.Reflection.Assembly.LoadFrom(dlg.FileName);
}
catch
{
}
}
else
throw new ArgumentException("Unable to find or load System.Data.SQLite.DLL");
}
return _assm;
}
set
{
_assm = value;
}
}
public InstallDesigner()
{
string[] args = Environment.GetCommandLineArgs();
for (int n = 0; n < args.Length; n++)
{
if (String.Compare(args[n], "/regroot", true) == 0 ||
String.Compare(args[n], "-regroot", true) == 0)
{
_regRoot = args[n + 1];
break;
}
else if (String.Compare(args[n], "/remove", true) == 0 ||
String.Compare(args[n], "-remove", true) == 0)
{
_remove = true;
}
}
InitializeComponent();
RegistryKey key;
using (key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft"))
{
AddItem(key, "VisualStudio", "Visual Studio (full editions)", standardDataProviderGuid, null);
AddItem(key, "VWDExpress", "Visual Web Developer Express Edition", standardDataProviderGuid, null);
warningPanel.Visible = (AddItem(key, "VCSExpress", "Visual C# Express Edition *", oledbDataProviderGuid, oledbAltDataProviderGuid)
| AddItem(key, "VCExpress", "Visual C++ Express Edition *", oledbDataProviderGuid, oledbAltDataProviderGuid)
| AddItem(key, "VBExpress", "Visual Basic Express Edition *", oledbDataProviderGuid, oledbAltDataProviderGuid)
| AddItem(key, "VJSExpress", "Visual J# Express Edition *", oledbDataProviderGuid, oledbAltDataProviderGuid));
}
GlobalAddRemove();
_ignoreChecks = false;
}
private bool AddItem(RegistryKey parent, string subkeyname, string itemName, Guid lookFor, object isChecked)
{
RegistryKey subkey;
try
{
using (subkey = parent.OpenSubKey(String.Format("{0}\\{1}", subkeyname, _regRoot)))
{
ListViewItem item = new ListViewItem(itemName);
item.Tag = subkeyname;
using (RegistryKey subsubkey = subkey.OpenSubKey("DataProviders"))
{
if (subsubkey == null)
throw new ArgumentException("Edition not installed");
}
using (RegistryKey subsubkey = subkey.OpenSubKey(String.Format("DataProviders\\{0}", (isChecked == null) ? lookFor.ToString("B") : ((Guid)isChecked).ToString("B"))))
{
if (subsubkey == null)
{
DoInstallUninstall(item);
}
else
{
bool itemChecked = (subsubkey.GetValue(null) != null);
DoInstallUninstall(item);
if (_remove == false) item.Checked = itemChecked;
}
}
installList.Items.Add(item);
if (item.Checked)
{
DoInstallUninstall(item);
}
return true;
}
}
catch
{
return false;
}
}
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void installList_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (_ignoreChecks) return;
DoInstallUninstall(e.Item);
GlobalAddRemove();
}
private void DoInstallUninstall(ListViewItem Item)
{
if (Item.Checked == false)
{
if (Item.Text.IndexOf('*') > -1)
RestoreJet((string)Item.Tag);
else
Uninstall((string)Item.Tag, standardDataProviderGuid, standardDataSourcesGuid);
}
else
{
if (Item.Text.IndexOf('*') > -1)
ReplaceJet((string)Item.Tag);
else
Install((string)Item.Tag, standardDataProviderGuid, standardDataSourcesGuid);
}
}
private void GlobalAddRemove()
{
bool install = false;
// bool installed;
//// Check to see if SQLite is installed in the GAC
//try
//{
// string file = AssemblyCache.QueryAssemblyInfo("System.Data.SQLite");
// installed = true;
//}
//catch
//{
// installed = false;
//}
// Check to see if any checkboxes in the list are checked
for (int n = 0; n < installList.Items.Count; n++)
{
if (installList.Items[n].Checked == true)
{
install = true;
break;
}
}
// If at least 1 item is checked, then install some global settings
if (install)
{
string path = Path.GetDirectoryName(SQLiteLocation);
using (RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\SQLite", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
key.SetValue(null, path);
}
while (String.IsNullOrEmpty(path) == false)
{
if (File.Exists(path + "\\CompactFramework\\System.Data.SQLite.DLL") == false)
{
path = Path.GetDirectoryName(path);
}
else break;
}
if (String.IsNullOrEmpty(path) == false)
{
path += "\\CompactFramework\\";
for (int n = 0; n < compactFrameworks.Length; n++)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(String.Format("Software\\Microsoft\\.NETCompactFramework\\v2.0.0.0\\{0}\\AssemblyFoldersEx", compactFrameworks[n]), true))
{
if (key != null)
{
using (RegistryKey subkey = key.CreateSubKey("SQLite", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
subkey.SetValue(null, path);
}
}
}
}
}
for (int n = 0; n < 2; n++)
{
// Add factory support to the machine.config file.
try
{
string xmlFileName = Environment.ExpandEnvironmentVariables(String.Format("%WinDir%\\Microsoft.NET\\{0}\\v2.0.50727\\CONFIG\\machine.config", (n == 0) ? "Framework" : "Framework64"));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(xmlFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/system.data/DbProviderFactories/add[@invariant=\"System.Data.SQLite\"]");
if (xmlNode == null)
{
XmlNode xmlConfig = xmlDoc.SelectSingleNode("configuration");
if (xmlConfig != null)
{
XmlNode xmlData = xmlConfig.SelectSingleNode("system.data");
if (xmlData == null)
{
xmlData = xmlDoc.CreateNode(XmlNodeType.Element, "system.data", "");
xmlConfig.AppendChild(xmlData);
}
XmlNode xmlParent = xmlData.SelectSingleNode("DbProviderFactories");
if (xmlParent == null)
{
xmlParent = xmlDoc.CreateNode(XmlNodeType.Element, "DbProviderFactories", "");
xmlData.AppendChild(xmlParent);
}
//xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "remove", "");
//xmlNode.Attributes.SetNamedItem(xmlDoc.CreateAttribute("invariant"));
//xmlParent.AppendChild(xmlNode);
//xmlNode.Attributes.GetNamedItem("invariant").Value = "System.Data.SQLite";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -