xxxxプログラマのメモ

先人に感謝と敬意:自分の困ったこと調べたことのメモ

Powershell上でC#コードの実行

C#からPowerShellコマンドの実行 - Qiita
yomon.hatenablog.com

$assemblies = (
 "System",
 "System.Xml",
 "System.Linq",
 "System.Xml.Linq"
)

$source = @"
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

public static class UpdateConfig
    {
        public static void UpdateLauncher(string path)
        {
            XDocument xdoc = XDocument.Load(path);
            XElement _target = xdoc.Descendants("add").Single(item => (item.Attribute("key") != null ? item.Attribute("key").Value.ToUpper() : null) == "AGENT");
            _target.SetAttributeValue("value", @"C:\hoge.exe");

            var setting = new XmlWriterSettings()
            {
                NewLineChars = Environment.NewLine,
                Indent = true,
                NewLineOnAttributes = false,
                OmitXmlDeclaration = false
            };

            using (var writer = XmlWriter.Create(path, setting))
            {
                xdoc.Save(writer);
            }

        }
    }
"@

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharp
[UpdateConfig]::UpdateLauncher($word)

ClickOnceの配布先変更

sumurai993.hatenablog.com
【ClickOnce】SetUp.exe内のアプリケーションのインストールURLの変更
How to: Enable ClickOnce Security Settings - Visual Studio 2015 | Microsoft Docs
2016-09-30

MageUI.exeの手順

  1. AppFIiles配下のConfigを変更
  2. mageuiでAppFIiles配下のManufestに署名
  3. RootのManufestを開いて設定変更
  4. RootのManufestとAppFIiles配下のManufestを連結して署名

Thanks!

コード分析修正

Dispose メソッドの実装 | Microsoft Docs

public class Resource : IDisposable   
{  
    private IntPtr nativeResource = Marshal.AllocHGlobal(100);  
    private AnotherResource managedResource = new AnotherResource();  
  
// Dispose() calls Dispose(true)  
    public void Dispose()  
    {  
        Dispose(true);  
        GC.SuppressFinalize(this);  
    }  
    // NOTE: Leave out the finalizer altogether if this class doesn't   
    // own unmanaged resources itself, but leave the other methods  
    // exactly as they are.   
    ~Resource()   
    {  
        // Finalizer calls Dispose(false)  
        Dispose(false);  
    }  
    // The bulk of the clean-up code is implemented in Dispose(bool)  
    protected virtual void Dispose(bool disposing)  
    {  
        if (disposing)   
        {  
            // free managed resources  
            if (managedResource != null)  
            {  
                managedResource.Dispose();  
                managedResource = null;  
            }  
        }  
        // free native resources if there are any.  
        if (nativeResource != IntPtr.Zero)   
        {  
            Marshal.FreeHGlobal(nativeResource);  
            nativeResource = IntPtr.Zero;  
        }  
    }  
}  

Thanks!