1 |
originally posted 2015-02-05 |
We have a ClickOnce application for which we are merging and/or embedding all of our dependency assemblies into a single assembly and obfuscating with Red Gate’s SmartAssembly. ClickOnce doesn’t know about the merging and embedding, so it includes the non-obfuscated individual assembly files when it publishes. It turns out that it’s easy to exclude these files from the deployment at publish-time from Visual Studio.
From Project properties …
Publish tab …
Application Files button …
Select “Exclude” for the Publish Status of the desired files …
Save project …
Publish.
To do this to an already-published ClickOnce application requires more work. Here’s a LINQPad script example to programmatically find a file, remove it and re-sign the appropriate files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/* Written with LINQPad Programmatically remove files from a published ClickOnce application using System.Diagnostics; using System.IO; using System.Xml; */ string sourceDir = @"C:\published\my_app_1_0_0_135\"; string manifestFilePath = sourceDir + @"\my_app.exe.manifest"; string applicationManifestFilePath = sourceDir + @"\my_app.application"; string mageFilePath = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\mage.exe"; string certFilePath = @"C:\certificates\MyAuthenticodeCertificate.pfx"; string certPassword = "foo"; //load .manifest XML var doc = XDocument.Load(manifestFilePath); //rename the files w/o the .deploy extension string[] filePaths = Directory.GetFiles(sourceDir,"*.deploy"); foreach(string filePath in filePaths) { string realName = filePath.Replace(".deploy",string.Empty); File.Move(filePath,realName); File.Delete(filePath); } //locate and remove the desired entry (e.g. the "MyComponentToRemove.dll" of dependency type of "install") XNamespace ns = "urn:schemas-microsoft-com:asm.v2"; doc.Descendants(ns + "dependentAssembly") .Where(x => (string)x.Attribute("dependencyType") == "install") .Where(x=> ((string)x.Attribute("codebase")).Equals("MyComponentToRemove.dll")) .Select(x => x.Parent) .Remove(); //save .manifest XML doc.Save(manifestFilePath); //re-sign .manifest Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = mageFilePath; startInfo.Arguments = string.Format("-update \"{0}\" -cf \"{1}\" -pwd \"{2}\"",manifestFilePath,certFilePath,certPassword); process.StartInfo = startInfo; process.Start(); //re-sign .application process = new System.Diagnostics.Process(); startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = mageFilePath; startInfo.Arguments = string.Format("-update \"{0}\" -appmanifest \"{1}\" -cf \"{2}\" -pwd \"{3}\"",applicationManifestFilePath,manifestFilePath,certFilePath,certPassword); process.StartInfo = startInfo; process.Start(); //rename files with .deploy extension foreach(string filePath in filePaths) { string realName = filePath.Replace(".deploy",string.Empty); File.Move(realName,filePath); File.Delete(realName); } |