diff --git a/App/CommonCommands.cs b/App/CommonCommands.cs
deleted file mode 100644
index 8f0ca6b..0000000
--- a/App/CommonCommands.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using PDFPatcher.Common;
-
-namespace PDFPatcher
-{
- static class CommonCommands
- {
- internal static void CreateShortcut() {
- var p = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
- var s = new ShortcutFile(FileHelper.CombinePath(p, Constants.AppEngName + ".exe")) {
- WorkingDirectory = p,
- Description = Constants.AppName
- };
- var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- s.Save(FileHelper.CombinePath(desktopPath, Constants.AppName + ".lnk"));
-
- FormHelper.InfoBox("已在桌面创建" + Constants.AppName + "的快捷方式。");
- }
-
- internal static void VisitHomePage() {
- System.Diagnostics.Process.Start(Constants.AppHomePage);
- }
- }
-}
diff --git a/App/Functions/AppOptionForm.Designer.cs b/App/Functions/AppOptionForm.Designer.cs
index dc7f03a..4e97c5e 100644
--- a/App/Functions/AppOptionForm.Designer.cs
+++ b/App/Functions/AppOptionForm.Designer.cs
@@ -34,33 +34,35 @@
this._LoadEntireFileBox = new System.Windows.Forms.RadioButton();
this.label12 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
+ this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
+ this._FontNameEncodingBox = new System.Windows.Forms.ComboBox();
this._TextEncodingBox = new System.Windows.Forms.ComboBox();
this._SaveAppSettingsBox = new System.Windows.Forms.CheckBox();
this._CreateShortcutButton = new System.Windows.Forms.Button();
- this._FontNameEncodingBox = new System.Windows.Forms.ComboBox();
- this.label1 = new System.Windows.Forms.Label();
+ this.groupBox3 = new System.Windows.Forms.GroupBox();
+ this._DefaultOverwritePdfFileBox = new System.Windows.Forms.CheckBox();
+ this._AddContextMenuBox = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
+ this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// _DocInfoEncodingBox
//
this._DocInfoEncodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this._DocInfoEncodingBox.FormattingEnabled = true;
- this._DocInfoEncodingBox.Location = new System.Drawing.Point(103, 46);
- this._DocInfoEncodingBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._DocInfoEncodingBox.Location = new System.Drawing.Point(77, 37);
this._DocInfoEncodingBox.Name = "_DocInfoEncodingBox";
- this._DocInfoEncodingBox.Size = new System.Drawing.Size(177, 23);
+ this._DocInfoEncodingBox.Size = new System.Drawing.Size(134, 20);
this._DocInfoEncodingBox.TabIndex = 3;
//
// label11
//
this.label11.AutoSize = true;
- this.label11.Location = new System.Drawing.Point(8, 50);
- this.label11.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+ this.label11.Location = new System.Drawing.Point(6, 40);
this.label11.Name = "label11";
- this.label11.Size = new System.Drawing.Size(82, 15);
+ this.label11.Size = new System.Drawing.Size(65, 12);
this.label11.TabIndex = 2;
this.label11.Text = "文档属性:";
//
@@ -68,19 +70,17 @@
//
this._BookmarkEncodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this._BookmarkEncodingBox.FormattingEnabled = true;
- this._BookmarkEncodingBox.Location = new System.Drawing.Point(389, 46);
- this._BookmarkEncodingBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._BookmarkEncodingBox.Location = new System.Drawing.Point(292, 37);
this._BookmarkEncodingBox.Name = "_BookmarkEncodingBox";
- this._BookmarkEncodingBox.Size = new System.Drawing.Size(177, 23);
+ this._BookmarkEncodingBox.Size = new System.Drawing.Size(134, 20);
this._BookmarkEncodingBox.TabIndex = 5;
//
// label10
//
this.label10.AutoSize = true;
- this.label10.Location = new System.Drawing.Point(295, 50);
- this.label10.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+ this.label10.Location = new System.Drawing.Point(221, 40);
this.label10.Name = "label10";
- this.label10.Size = new System.Drawing.Size(82, 15);
+ this.label10.Size = new System.Drawing.Size(65, 12);
this.label10.TabIndex = 4;
this.label10.Text = "书签文本:";
//
@@ -88,11 +88,9 @@
//
this.groupBox1.Controls.Add(this._LoadPartialFileBox);
this.groupBox1.Controls.Add(this._LoadEntireFileBox);
- this.groupBox1.Location = new System.Drawing.Point(16, 42);
- this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this.groupBox1.Location = new System.Drawing.Point(12, 61);
this.groupBox1.Name = "groupBox1";
- this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
- this.groupBox1.Size = new System.Drawing.Size(573, 58);
+ this.groupBox1.Size = new System.Drawing.Size(449, 46);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "访问 PDF 文档";
@@ -100,10 +98,9 @@
// _LoadPartialFileBox
//
this._LoadPartialFileBox.AutoSize = true;
- this._LoadPartialFileBox.Location = new System.Drawing.Point(273, 25);
- this._LoadPartialFileBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._LoadPartialFileBox.Location = new System.Drawing.Point(223, 20);
this._LoadPartialFileBox.Name = "_LoadPartialFileBox";
- this._LoadPartialFileBox.Size = new System.Drawing.Size(268, 19);
+ this._LoadPartialFileBox.Size = new System.Drawing.Size(215, 16);
this._LoadPartialFileBox.TabIndex = 1;
this._LoadPartialFileBox.TabStop = true;
this._LoadPartialFileBox.Text = "减少占用内存(仅加载需处理部分)";
@@ -112,10 +109,9 @@
// _LoadEntireFileBox
//
this._LoadEntireFileBox.AutoSize = true;
- this._LoadEntireFileBox.Location = new System.Drawing.Point(11, 25);
- this._LoadEntireFileBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._LoadEntireFileBox.Location = new System.Drawing.Point(8, 20);
this._LoadEntireFileBox.Name = "_LoadEntireFileBox";
- this._LoadEntireFileBox.Size = new System.Drawing.Size(238, 19);
+ this._LoadEntireFileBox.Size = new System.Drawing.Size(191, 16);
this._LoadEntireFileBox.TabIndex = 0;
this._LoadEntireFileBox.TabStop = true;
this._LoadEntireFileBox.Text = "优化处理效率(加载整个文件)";
@@ -125,10 +121,9 @@
//
this.label12.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
- this.label12.Location = new System.Drawing.Point(8, 21);
- this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+ this.label12.Location = new System.Drawing.Point(6, 17);
this.label12.Name = "label12";
- this.label12.Size = new System.Drawing.Size(560, 21);
+ this.label12.Size = new System.Drawing.Size(437, 17);
this.label12.TabIndex = 6;
this.label12.Text = "说明:当遇到 PDF 文档的文本为乱码时,可尝试使用此选项强制设定编码。";
//
@@ -143,97 +138,122 @@
this.groupBox2.Controls.Add(this.label12);
this.groupBox2.Controls.Add(this._BookmarkEncodingBox);
this.groupBox2.Controls.Add(this.label11);
- this.groupBox2.Location = new System.Drawing.Point(13, 108);
- this.groupBox2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this.groupBox2.Location = new System.Drawing.Point(12, 113);
this.groupBox2.Name = "groupBox2";
- this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
- this.groupBox2.Size = new System.Drawing.Size(576, 114);
+ this.groupBox2.Size = new System.Drawing.Size(449, 91);
this.groupBox2.TabIndex = 9;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "读取文档所用的编码";
//
+ // label1
+ //
+ this.label1.AutoSize = true;
+ this.label1.Location = new System.Drawing.Point(220, 66);
+ this.label1.Name = "label1";
+ this.label1.Size = new System.Drawing.Size(65, 12);
+ this.label1.TabIndex = 7;
+ this.label1.Text = "字体名称:";
+ //
// label2
//
this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(8, 82);
- this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+ this.label2.Location = new System.Drawing.Point(6, 66);
this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(82, 15);
+ this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 7;
this.label2.Text = "正文文本:";
//
+ // _FontNameEncodingBox
+ //
+ this._FontNameEncodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
+ this._FontNameEncodingBox.FormattingEnabled = true;
+ this._FontNameEncodingBox.Location = new System.Drawing.Point(292, 63);
+ this._FontNameEncodingBox.Name = "_FontNameEncodingBox";
+ this._FontNameEncodingBox.Size = new System.Drawing.Size(134, 20);
+ this._FontNameEncodingBox.TabIndex = 3;
+ //
// _TextEncodingBox
//
this._TextEncodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this._TextEncodingBox.FormattingEnabled = true;
- this._TextEncodingBox.Location = new System.Drawing.Point(103, 79);
- this._TextEncodingBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._TextEncodingBox.Location = new System.Drawing.Point(77, 63);
this._TextEncodingBox.Name = "_TextEncodingBox";
- this._TextEncodingBox.Size = new System.Drawing.Size(177, 23);
+ this._TextEncodingBox.Size = new System.Drawing.Size(134, 20);
this._TextEncodingBox.TabIndex = 3;
//
// _SaveAppSettingsBox
//
this._SaveAppSettingsBox.AutoSize = true;
- this._SaveAppSettingsBox.Location = new System.Drawing.Point(16, 15);
- this._SaveAppSettingsBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._SaveAppSettingsBox.Location = new System.Drawing.Point(12, 12);
this._SaveAppSettingsBox.Name = "_SaveAppSettingsBox";
- this._SaveAppSettingsBox.Size = new System.Drawing.Size(179, 19);
+ this._SaveAppSettingsBox.Size = new System.Drawing.Size(144, 16);
this._SaveAppSettingsBox.TabIndex = 11;
this._SaveAppSettingsBox.Text = "自动保存应用程序设置";
this._SaveAppSettingsBox.UseVisualStyleBackColor = true;
//
// _CreateShortcutButton
//
- this._CreateShortcutButton.Location = new System.Drawing.Point(311, 10);
- this._CreateShortcutButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+ this._CreateShortcutButton.Location = new System.Drawing.Point(299, 8);
this._CreateShortcutButton.Name = "_CreateShortcutButton";
- this._CreateShortcutButton.Size = new System.Drawing.Size(216, 29);
+ this._CreateShortcutButton.Size = new System.Drawing.Size(162, 23);
this._CreateShortcutButton.TabIndex = 12;
this._CreateShortcutButton.Text = "在桌面创建程序快捷方式";
this._CreateShortcutButton.UseVisualStyleBackColor = true;
this._CreateShortcutButton.Click += new System.EventHandler(this._CreateShortcutButton_Click);
//
- // _FontNameEncodingBox
+ // groupBox3
//
- this._FontNameEncodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this._FontNameEncodingBox.FormattingEnabled = true;
- this._FontNameEncodingBox.Location = new System.Drawing.Point(389, 79);
- this._FontNameEncodingBox.Margin = new System.Windows.Forms.Padding(4);
- this._FontNameEncodingBox.Name = "_FontNameEncodingBox";
- this._FontNameEncodingBox.Size = new System.Drawing.Size(177, 23);
- this._FontNameEncodingBox.TabIndex = 3;
+ this.groupBox3.Controls.Add(this._DefaultOverwritePdfFileBox);
+ this.groupBox3.Location = new System.Drawing.Point(12, 210);
+ this.groupBox3.Name = "groupBox3";
+ this.groupBox3.Size = new System.Drawing.Size(449, 47);
+ this.groupBox3.TabIndex = 13;
+ this.groupBox3.TabStop = false;
+ this.groupBox3.Text = "编辑器";
//
- // label1
+ // _DefaultOverwritePdfFileBox
//
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(294, 82);
- this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(82, 15);
- this.label1.TabIndex = 7;
- this.label1.Text = "字体名称:";
+ this._DefaultOverwritePdfFileBox.AutoSize = true;
+ this._DefaultOverwritePdfFileBox.Location = new System.Drawing.Point(8, 20);
+ this._DefaultOverwritePdfFileBox.Name = "_DefaultOverwritePdfFileBox";
+ this._DefaultOverwritePdfFileBox.Size = new System.Drawing.Size(186, 16);
+ this._DefaultOverwritePdfFileBox.TabIndex = 0;
+ this._DefaultOverwritePdfFileBox.Text = "默认覆盖正在编辑的 PDF 文件";
+ this._DefaultOverwritePdfFileBox.UseVisualStyleBackColor = true;
//
- // AppOptionControl
+ // _AddContextMenuBox
//
- this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
+ this._AddContextMenuBox.AutoSize = true;
+ this._AddContextMenuBox.Location = new System.Drawing.Point(20, 36);
+ this._AddContextMenuBox.Name = "_AddContextMenuBox";
+ this._AddContextMenuBox.Size = new System.Drawing.Size(264, 16);
+ this._AddContextMenuBox.TabIndex = 14;
+ this._AddContextMenuBox.Text = "PDF 文件右键菜单的“打开方式”添加“PDF补丁丁”";
+ this._AddContextMenuBox.UseVisualStyleBackColor = true;
+ //
+ // AppOptionForm
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(631, 334);
+ this.ClientSize = new System.Drawing.Size(473, 267);
+ this.Controls.Add(this._AddContextMenuBox);
+ this.Controls.Add(this.groupBox3);
this.Controls.Add(this._CreateShortcutButton);
this.Controls.Add(this._SaveAppSettingsBox);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
- this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+ this.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.MaximizeBox = false;
this.MinimizeBox = false;
- this.Name = "AppOptionControl";
+ this.Name = "AppOptionForm";
this.Text = "程序工作选项";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
+ this.groupBox3.ResumeLayout(false);
+ this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@@ -256,5 +276,8 @@
private System.Windows.Forms.Button _CreateShortcutButton;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox _FontNameEncodingBox;
+ private System.Windows.Forms.GroupBox groupBox3;
+ private System.Windows.Forms.CheckBox _DefaultOverwritePdfFileBox;
+ private System.Windows.Forms.CheckBox _AddContextMenuBox;
}
}
diff --git a/App/Functions/AppOptionForm.cs b/App/Functions/AppOptionForm.cs
index 450cac8..25bbb80 100644
--- a/App/Functions/AppOptionForm.cs
+++ b/App/Functions/AppOptionForm.cs
@@ -20,6 +20,8 @@ namespace PDFPatcher.Functions
_FontNameEncodingBox.SelectedIndexChanged += ControlChanged;
_SaveAppSettingsBox.CheckedChanged += ControlChanged;
_LoadEntireFileBox.CheckedChanged += ControlChanged;
+ _DefaultOverwritePdfFileBox.CheckedChanged += ControlChanged;
+ _AddContextMenuBox.CheckedChanged += ControlChanged;
}
public void Reset() {
@@ -34,6 +36,8 @@ namespace PDFPatcher.Functions
_SaveAppSettingsBox.Checked = AppContext.SaveAppSettings;
_LoadPartialFileBox.Checked = AppContext.LoadPartialPdfFile;
_LoadEntireFileBox.Checked = !AppContext.LoadPartialPdfFile;
+ _DefaultOverwritePdfFileBox.Checked = AppContext.Editor.DefaultOverwriteDocument;
+ _AddContextMenuBox.Checked = ShellHelper.CheckPdfOpenWithAssociation();
InitEncodingList(_BookmarkEncodingBox, AppContext.Encodings.BookmarkEncodingName);
InitEncodingList(_DocInfoEncodingBox, AppContext.Encodings.DocInfoEncodingName);
@@ -79,11 +83,21 @@ namespace PDFPatcher.Functions
else if (sender == _LoadEntireFileBox) {
AppContext.LoadPartialPdfFile = _LoadPartialFileBox.Checked;
}
+ else if (sender == _DefaultOverwritePdfFileBox) {
+ AppContext.Editor.DefaultOverwriteDocument = _DefaultOverwritePdfFileBox.Checked;
+ }
+ else if (sender == _AddContextMenuBox) {
+ if (_AddContextMenuBox.Checked) {
+ ShellHelper.AddPdfOpenWithAssociation();
+ }
+ else {
+ ShellHelper.RemovePdfOpenWithAssociation();
+ }
+ }
}
void _CreateShortcutButton_Click(object sender, EventArgs e) {
- CommonCommands.CreateShortcut();
+ ShellHelper.CreateShortcut();
}
-
}
}
diff --git a/App/Functions/UpdateForm.cs b/App/Functions/UpdateForm.cs
index ac3038e..57e95a5 100644
--- a/App/Functions/UpdateForm.cs
+++ b/App/Functions/UpdateForm.cs
@@ -28,7 +28,7 @@ namespace PDFPatcher.Functions
}
};
_HomePageButton.Click += (s, args) => {
- CommonCommands.VisitHomePage();
+ ShellHelper.VisitHomePage();
};
_DownloadButton.Click += (s, args) => {
System.Diagnostics.Process.Start(_DownloadButton.Tag.ToString());
diff --git a/App/MainForm.cs b/App/MainForm.cs
index 2217f39..f6564ed 100644
--- a/App/MainForm.cs
+++ b/App/MainForm.cs
@@ -388,10 +388,10 @@ namespace PDFPatcher
ShowLogControl();
}
else if (commandName == Commands.CreateShortcut) {
- CommonCommands.CreateShortcut();
+ ShellHelper.CreateShortcut();
}
else if (commandName == Commands.VisitHomePage) {
- CommonCommands.VisitHomePage();
+ ShellHelper.VisitHomePage();
}
else if (commandName == Commands.CheckUpdate) {
ShowDialogWindow(new UpdateForm());
diff --git a/App/Options/PatcherOptions.cs b/App/Options/PatcherOptions.cs
index 4db126e..00b7357 100644
--- a/App/Options/PatcherOptions.cs
+++ b/App/Options/PatcherOptions.cs
@@ -8,6 +8,8 @@ namespace PDFPatcher
[XmlRoot("文档设置")]
public class PatcherOptions : DocumentOptions
{
+ [XmlAttribute("默认覆盖文档")]
+ public bool DefaultOverwriteDocument { get; set; }
[XmlAttribute("嵌入字库")]
public bool EmbedFonts { get; set; }
[XmlAttribute("删除文本尾随空白")]
diff --git a/App/PDFPatcher.csproj b/App/PDFPatcher.csproj
index 67f855d..b35b194 100644
--- a/App/PDFPatcher.csproj
+++ b/App/PDFPatcher.csproj
@@ -94,7 +94,8 @@
Configuration.resx
-
+
+
diff --git a/App/ShellHelper.cs b/App/ShellHelper.cs
new file mode 100644
index 0000000..59b326a
--- /dev/null
+++ b/App/ShellHelper.cs
@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using PDFPatcher.Common;
+using Microsoft.Win32;
+
+namespace PDFPatcher
+{
+ static class ShellHelper
+ {
+ internal static void CreateShortcut() {
+ var p = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
+ var s = new ShortcutFile(FileHelper.CombinePath(p, Constants.AppEngName + ".exe")) {
+ WorkingDirectory = p,
+ Description = Constants.AppName
+ };
+ var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
+ s.Save(FileHelper.CombinePath(desktopPath, Constants.AppName + ".lnk"));
+
+ FormHelper.InfoBox("已在桌面创建" + Constants.AppName + "的快捷方式。");
+ }
+
+ internal static void VisitHomePage() {
+ System.Diagnostics.Process.Start(Constants.AppHomePage);
+ }
+
+ internal static bool CheckPdfOpenWithAssociation() {
+ try {
+ return OpenWithAssociation.HasAssociation();
+ }
+ catch (Exception) {
+ // ignore
+ return false;
+ }
+ }
+
+ internal static void AddPdfOpenWithAssociation() {
+ try {
+ OpenWithAssociation.Create();
+ }
+ catch (Exception ex) {
+ FormHelper.ErrorBox(ex.ToString());
+ }
+ }
+
+ internal static void RemovePdfOpenWithAssociation() {
+ try {
+ OpenWithAssociation.Delete();
+ }
+ catch (Exception ex) {
+ FormHelper.ErrorBox(ex.ToString());
+ }
+ }
+
+ static RegistryKey OpenOrCreateSubKey(this RegistryKey baseKey, string keyPath, bool createIfInexistent) {
+ var key = baseKey.OpenSubKey(keyPath, true);
+ return key == null && createIfInexistent
+ ? baseKey.CreateSubKey(keyPath)
+ : key;
+ }
+
+ sealed class OpenWithAssociation
+ {
+ const string DocumentType = Constants.AppEngName + ".Document";
+ const string ShellCommandKey = "OpenWith" + Constants.AppEngName;
+ const string ProgId = "PDF.Document";
+
+ public static bool HasAssociation() {
+ using (var classes = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes"))
+ using (var fileType = classes.OpenSubKey(Constants.FileExtensions.Pdf, true)) {
+ if (fileType == null) {
+ return false;
+ }
+ var progIdName = fileType.GetValue(String.Empty, null) as string;
+ if (progIdName == ProgId) {
+ return true;
+ }
+ using (var command = classes.OpenSubKey($@"{progIdName}\shell\{ShellCommandKey}\command")) {
+ return command != null;
+ }
+ }
+ }
+
+ public static void Create() {
+ using (var classes = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes", true))
+ using (var fileType = classes.OpenOrCreateSubKey(Constants.FileExtensions.Pdf, true)) {
+ var progIdName = fileType.GetValue(String.Empty, null) as string;
+ if (String.IsNullOrEmpty(progIdName)) {
+ fileType.SetValue(String.Empty, progIdName = ProgId);
+ }
+ using (var progId = classes.OpenOrCreateSubKey(progIdName, true))
+ using (var shell = progId.OpenOrCreateSubKey("shell", true))
+ using (var openWith = shell.OpenOrCreateSubKey(ShellCommandKey, true))
+ using (var command = openWith.OpenOrCreateSubKey("command", true)) {
+ openWith.SetValue(String.Empty, "使用 " + Constants.AppName + " 打开");
+ command.SetValue(String.Empty, $"\"{FilePath.AppPath}\" \"%1\"");
+ }
+ }
+ }
+
+ public static void Delete() {
+ using (var classes = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes", true))
+ using (var fileType = classes.OpenSubKey(Constants.FileExtensions.Pdf, true)) {
+ if (fileType == null) {
+ return;
+ }
+ var progIdName = fileType.GetValue(String.Empty, null) as string;
+ if (String.IsNullOrEmpty(progIdName)) {
+ return;
+ }
+ if (progIdName == ProgId) {
+ classes.DeleteSubKeyTree(progIdName, false);
+ return;
+ }
+ using (var shell = classes.OpenSubKey(progIdName + "\\shell", true))
+ using (var openWith = shell.OpenOrCreateSubKey(ShellCommandKey, true)) {
+ shell.DeleteSubKeyTree(ShellCommandKey, false);
+ }
+ }
+ }
+ }
+ }
+}