Excel催化剂开源第4波-ClickOnce部署要点之导入数字证书及创建EXCEL信任文件夹
Excel催化劉插件使用Clickonce的部署方式發布插件,以滿足用戶使用插件過程中,需要對插件進行功能升級時,可以無痛地自動更新推送新版本。
但Clickonce部署,對用戶環境有較大的要求,前期首次安裝,比較波折,但相對于后續的自動更新的回報,筆者自我感覺還是很值得的。
Clickonce部署過程中,要求導入數字證書和設置Excel共享路徑這兩個步驟,本篇開源代碼主要講述這個過程的自動化處理的代碼實現,同樣用的是Console程序。
為了還原一個干凈無侵擾的網絡世界,本文將不進行大規模地分發,若您覺得此文有用,不妨小范圍地分享到真正有需要的人手中
關于Clickonce部署的其他介紹
若對Clickonce部署的其他深入知識點,可以通過百度自行補充或通過以下鏈接繼續深入學習。
ClickOnce部署 - 無恨星晨 - 博客園?http://www.cnblogs.com/weixing/p/3358740.html
Excel催化劑公眾號歷史文章
https://mp.weixin.qq.com/s/HCluSw-8uZkXiLWBeeJqiA
https://mp.weixin.qq.com/s/G8B2gEG8LfIUCuSyAPFX2w
代碼實現原理
- 導入數據證書
預先把證書放到資源里,然后調用Windows證書導入的類庫的一些命令即可。 - 創建信任位置
此操作也是在注冊表上完成,在注冊表上新建一個條目,指向要共享的路徑即可。
同樣的因筆者非專業程序猿,可能寫出來的代碼嚴謹性有限,僅供參考。
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Text.RegularExpressions; namespace 導入證書及設置EXCEL信任文件夾 {class Program{static void Main(string[] args) { //64位系統 try { Console.WriteLine("正在導入證書操作"); X509Store storeTrustedPublisher = new X509Store(StoreName.TrustedPublisher, StoreLocation.CurrentUser); //導入外部信任者 ImportCertificate(storeTrustedPublisher); //導入根證書信任 X509Store storeRoot = new X509Store(StoreName.Root, StoreLocation.CurrentUser); ImportCertificate(storeRoot); Console.WriteLine("正在創建EXCEL信任文件夾"); TrustDirSetting.SettingTrustDir("http://LiWeiJianWeb/"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("操作完成,請按任意鍵結束!"); Console.ReadKey(); } } private static void CreateTargetSubKey(RegisterManager registerManager, List<string> listSubKeys, RegistryKey localKey) { var regAllowNetworkLocations = listSubKeys.Where(s => s.EndsWith(@"Excel\Security\Trusted Locations")); //設置信任網絡路徑 foreach (var item in regAllowNetworkLocations) { registerManager.SetRegeditKeyValue(item, "AllowNetworkLocations", "1"); } //包含EXCEL字樣的,并且有location節點 var listSecurity = listSubKeys.Where(s => s.Contains(@"Excel\Security\Trusted Locations")).Where(s => Regex.IsMatch(s, @"Location\d+$")).ToList(); foreach (var item in listSecurity) { if (registerManager.IsRegeditKeyAndValueExist(item, "Path", @"http://LiWeiJianWeb/")) { return; } }; var result = from s in listSecurity select new { GroupName = Regex.Match(s, @".+?\\.+?\\.+?\\.+?\\").Value, Fullpath = s }; //按HKEY_CURRENT_USER\Software\Microsoft\Office\15.0分組,防止多個EXCEL版本的原因引起信任位置添加不全 var query = from s in result group s by s.GroupName; foreach (var item in query) { //只取第1條記錄,去掉最后一個尾數 string locationName = Regex.Match(item.First().Fullpath, @".+Location").Value; //用最后的尾數來比較大小,不是用字符串,可以最終比較出11比2大 int locationIndex = item.Max(s => int.Parse(Regex.Match(s.Fullpath, @".+Location(\d+)").Groups[1].Value) + 1); string newLocationName = Regex.Match(locationName, ".+Location").Value + locationIndex; RegistryKey Location = localKey.CreateSubKey(newLocationName); Location.SetValue("Path", @"http://LiWeiJianWeb/"); Location.SetValue("AllowSubfolders", "00000001", RegistryValueKind.DWord); Location.SetValue("Date", DateTime.Now.ToString()); Location.SetValue("Description", ""); } } private static void ImportCertificate(X509Store store) { store.Open(OpenFlags.ReadWrite); X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "Excel催化劑", false); if (certs.Count == 0 || certs[0].NotAfter < DateTime.Now) { X509Certificate2 certificate = new X509Certificate2(Resource1.Excel催化劑); store.Remove(certificate); //可省略 store.Add(certificate); store.Close(); } } } }作了個幫助類
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace 導入證書及設置EXCEL信任文件夾 { class TrustDirSetting { public static void SettingTrustDir(string trustDir) { if (Environment.Is64BitOperatingSystem) { //64位的EXCEL AddTrustDirToRegister(trustDir, RegistryView.Registry64); } //32位的EXCEL AddTrustDirToRegister(trustDir, RegistryView.Registry32); } private static void AddTrustDirToRegister(string trustDir, RegistryView registryView) { List<string> listSubKeys = new List<string>(); var localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView); RegisterManager registerManager = new RegisterManager() { BaseKey = localKey }; registerManager.EnumerateKeyNames(@"Software\Microsoft\Office", ref listSubKeys); CreateTargetSubKey(registerManager, listSubKeys, localKey, trustDir); } private static void CreateTargetSubKey(RegisterManager registerManager, List<string> listSubKeys, RegistryKey localKey, string trustDir) { var regAllowNetworkLocations = listSubKeys.Where(s => s.EndsWith(@"Excel\Security\Trusted Locations")); //設置信任網絡路徑 foreach (var item in regAllowNetworkLocations) { registerManager.SetRegeditKeyValue(item, "AllowNetworkLocations", "1"); } //包含EXCEL字樣的,并且有location節點 var listSecurity = listSubKeys.Where(s => s.Contains(@"Excel\Security\Trusted Locations")).Where(s => Regex.IsMatch(s, @"Location\d+$")).ToList(); foreach (var item in listSecurity) { if (registerManager.IsRegeditKeyAndValueExist(item, "Path", trustDir)) { return; } }; var result = from s in listSecurity select new { GroupName = Regex.Match(s, @".+?\\.+?\\.+?\\.+?\\").Value, Fullpath = s }; //按HKEY_CURRENT_USER\Software\Microsoft\Office\15.0分組,防止多個EXCEL版本的原因引起信任位置添加不全 var query = from s in result group s by s.GroupName; foreach (var item in query) { //只取第1條記錄,去掉最后一個尾數 string locationName = Regex.Match(item.First().Fullpath, @".+Location").Value; //用最后的尾數來比較大小,不是用字符串,可以最終比較出11比2大 int locationIndex = item.Max(s => int.Parse(Regex.Match(s.Fullpath, @".+Location(\d+)").Groups[1].Value) + 1); string newLocationName = Regex.Match(locationName, ".+Location").Value + locationIndex; RegistryKey Location = localKey.CreateSubKey(newLocationName); Location.SetValue("Path", trustDir); Location.SetValue("AllowSubfolders", "00000001", RegistryValueKind.DWord); Location.SetValue("Date", DateTime.Now.ToString()); Location.SetValue("Description", ""); } } } internal class RegisterManager { public RegistryKey BaseKey { get; set; } private bool IsRegeditKeyExist(string subKeyString, string key) { string[] subkeyNames; RegistryKey subKey = this.BaseKey.OpenSubKey(subKeyString); subkeyNames = subKey.GetValueNames(); //取得該項下所有鍵值的名稱的序列,并傳遞給預定的數組中 foreach (string keyName in subkeyNames) { if (keyName == key) //判斷鍵值的名稱 { return true; } } return false; } public bool IsRegeditKeyAndValueExist(string subKeyString, string key, string valueString) { string[] subkeyNames; RegistryKey subKey = this.BaseKey.OpenSubKey(subKeyString); subkeyNames = subKey.GetValueNames(); //取得該項下所有鍵值的名稱的序列,并傳遞給預定的數組中 foreach (string keyName in subkeyNames) { if (keyName == key) //判斷鍵值的名稱 { if (subKey.GetValue(key).ToString() == valueString) { return true; } } } return false; } public void SetRegeditKeyValue(string subKeyString, string key, string valueString) { RegistryKey subKey = this.BaseKey.OpenSubKey(subKeyString, true); subKey.SetValue(key, valueString, RegistryValueKind.DWord); } public void EnumerateKeyNames(string fatherKey, ref List<string> listSubKeys) { RegistryKey RegKey = this.BaseKey.OpenSubKey(fatherKey); string[] subKeys = RegKey.GetSubKeyNames(); foreach (string subKey in subKeys) { string fullPath = fatherKey + "\\" + subKey; EnumerateKeyNames(fullPath, ref listSubKeys); listSubKeys.Add(fullPath); } } } }開源地址為:https://github.com/minren118/ExcelUdfByExcelCuiHuaJi,不妨對您有幫助時幫忙在GtiHub上點個贊。
登錄Github后點擊紅框給個星星技術交流QQ群
QQ群名:Excel催化劑開源討論群, QQ群號:788145319
?
Excel催化劑開源討論群二維碼關于Excel催化劑
Excel催化劑先是一微信公眾號的名稱,后來順其名稱,正式推出了Excel插件,插件將持續性地更新,更新的周期視本人的時間而定爭取一周能夠上線一個大功能模塊。Excel催化劑插件承諾個人用戶永久性免費使用!
Excel催化劑插件使用最新的布署技術,實現一次安裝,日后所有更新自動更新完成,無需重復關注更新動態,手動下載安裝包重新安裝,只需一次安裝即可隨時保持最新版本!
Excel催化劑插件下載鏈接:https://pan.baidu.com/s/1Iz2_NZJ8v7C9eqhNjdnP3Q
聯系作者 公眾號取名催化劑,因Excel本身的強大,并非所有人能夠立馬享受到,大部分人還是在被Excel軟件所虐的階段,就是頭腦里很清晰想達到的效果,而且高手們也已經實現出來,就是自己怎么弄都弄不出來,或者更糟的是還不知道Excel能夠做什么而停留在不斷地重復、機械、手工地在做著數據,耗費著無數的青春年華歲月。所以催生了是否可以作為一種媒介,讓廣大的Excel用戶們可以瞬間點燃Excel的爆點,無需苦苦地掙扎地沒日沒夜的技巧學習、高級復雜函數的燒腦,最終走向了從入門到放棄的道路。
最后Excel功能強大,其實還需樹立一個觀點,不是所有事情都要交給Excel去完成,也不是所有事情Excel都是十分勝任的,外面的世界仍然是一個廣闊的世界,Excel只是其中一枚耀眼的明星,還有其他更多同樣精彩強大的技術、工具等。*Excel催化劑也將借力這些其他技術,讓Excel能夠發揮更強大的爆發!
關于Excel催化劑作者
姓名:李偉堅,從事數據分析工作多年(BI方向),一名同樣在路上的學習者。
服務過行業:零售特別是鞋服類的零售行業,電商(淘寶、天貓、京東、唯品會)
技術路線從一名普通用戶,通過Excel軟件的學習,從此走向數據世界,非科班IT專業人士。
歷經重重難關,終于在數據的道路上達到技術平原期,學習眾多的知識不再太吃力,同時也形成了自己的一套數據解決方案(數據采集、數據加工清洗、數據多維建模、數據報表展示等)。
擅長技術領域:Excel等Office家族軟件、VBA&VSTO的二次開發、Sqlserver數據庫技術、Sqlserver的商業智能BI技術、Powerbi技術、云服務器布署技術等等。
2018年開始職業生涯作了重大調整,從原來的正職工作,轉為自由職業者,暫無固定收入,暫對前面道路不太明朗,苦重新回到正職工作,對Excel催化劑的運營和開發必定受到很大的影響(正職工作時間內不可能維護也不可能隨便把工作時間內的成果公布于外,工作外的時間也十分有限,因已而立之年,家庭責任重大)。
和廣大擁護者一同期盼:Excel催化劑一直能運行下去,我所惠及的群體們能夠給予支持(多留言鼓勵下、轉發下朋友圈推薦、小額打賞下和最重點的可以和所在公司及同行推薦推薦,讓我的技術可以在貴司發揮價值,實現雙贏(初步設想可以數據顧問的方式或一些小型項目開發的方式合作)。
轉載于:https://www.cnblogs.com/ExcelCuiHuaJi/p/10232335.html
總結
以上是生活随笔為你收集整理的Excel催化剂开源第4波-ClickOnce部署要点之导入数字证书及创建EXCEL信任文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “约见”面试官系列之常见面试题之第八十五
- 下一篇: eoLinker-API_Shop_知识