这是我的实现。我花了很多时间在这一小段代码上,但我仍然没有找到让mage在不干预的情况下处理所有生成的.application文件的所有正确选项。我要说的是,可能会对这段代码进行很多优化。然而,这仍然可以作为跳板来帮助别人。
为了使以下方法工作,您必须从vs中的clickOnce至少部署一次,然后只保留.application文件不进行部署。必须删除Deploy文件夹中的.Application和.Manifest。
在我将所有应用程序文件移动到
Config.Instance.ServerSettings.ClientLocation + "<AppName>_<version>"
:
DirectoryInfo filedir = new DirectoryInfo(Config.Instance.ServerSettings.ClientLocation);
if (filedir.Exists)
{
FileInfo[] files = filedir.GetFiles();
FileInfo appinfo = null;
foreach (FileInfo fi in files)
{
if (fi.Name == "<AppName>.application")
{
appinfo = fi;
break;
}
}
if (appinfo != null)
{
XmlDocument applocinfo = new XmlDocument();
applocinfo.Load(appinfo.FullName);
string codebase = applocinfo["asmv1:assembly"]["dependency"]["dependentAssembly"].Attributes["codebase"].Value.Replace("AppName.exe.manifest", "");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Path.Combine(Path.Combine(filedir.FullName, codebase), "AppName.exe.config"));
foreach (XmlNode xn in xDoc["configuration"]["appSettings"].ChildNodes)
{
if (xn.Attributes != null && xn.Attributes["key"] != null && xn.Attributes["key"].Value == "Clnt_Host")
{
xn.Attributes["value"].Value = Config.Instance.ClientSettings.Host;
break;
}
}
xDoc.Save(Path.Combine(Path.Combine(filedir.FullName, codebase), "<AppName>.exe.config"));
Process p = new Process();
p.StartInfo = new ProcessStartInfo(Path.Combine(filedir.FullName, "Mage.exe"));
p.StartInfo.WorkingDirectory = filedir.FullName;
FileInfo fi = new FileInfo(Path.Combine(Path.Combine(filedir.FullName, codebase.TrimStart('.')), "<AppName>.exe.manifest"));
if (fi.Exists)
fi.Delete();
p.StartInfo.Arguments = "-new Application -fd \".\\" + codebase.TrimEnd('\\') + "\" -ToFile \".\\" + Path.Combine(codebase, "<AppName>.exe.manifest") + "\" -Name \"<AppName>\" -Version \"" + codebase.Substring(codebase.IndexOf('_') + 1, codebase.Length - (codebase.IndexOf('_') + 1)).Replace('_', '.').TrimEnd('\\') + "\" -CertFile \"<KeyName>.pfx\" -Password <Password> -IconFile \"64x64.ico\"";
while (p.StartInfo.Arguments.Contains(".\\.\\"))
p.StartInfo.Arguments = p.StartInfo.Arguments.Replace(".\\.\\", ".\\");
Logger.Instance.LogInfo("Starting application: " + p.StartInfo.FileName + "\n\tWith arguments: " + p.StartInfo.Arguments, Logger.InfoType.Information);
p.Start();
while (!p.HasExited)
{
Thread.Sleep(100);
}
p.StartInfo.Arguments = "-new Deployment -I t -t \"<AppName>.application\" -v \"" + codebase.Substring(codebase.IndexOf('_') + 1, codebase.Length - (codebase.IndexOf('_') + 1)).Replace('_', '.').TrimEnd('\\') + "\" -AppManifest \".\\" + codebase + "<AppName>.exe.manifest\" -pu \"http://" + Config.Instance.ClientSettings.Host + "/client/" + codebase.Replace('\\', '/') + "<AppName>.exe.manifest\"";
while (p.StartInfo.Arguments.Contains(".\\.\\"))
p.StartInfo.Arguments = p.StartInfo.Arguments.Replace(".\\.\\", ".\\");
Logger.Instance.LogInfo("Starting application: " + p.StartInfo.FileName + "\n\tWith arguments: " + p.StartInfo.Arguments, Logger.InfoType.Information);
p.Start();
while (!p.HasExited)
{
Thread.Sleep(100);
}
xDoc = new XmlDocument();
xDoc.Load(Path.Combine(filedir.FullName, "<AppName>.application"));
if (xDoc["asmv1:assembly"]["deployment"]["subscription"] != null)
{
xDoc["asmv1:assembly"]["deployment"].RemoveChild(xDoc["asmv1:assembly"]["deployment"]["subscription"]);
xDoc["asmv1:assembly"]["deployment"].RemoveChild(xDoc["asmv1:assembly"]["deployment"]["deploymentProvider"]);
XmlAttribute node = xDoc.CreateAttribute("minimumRequiredVersion");
node.Value = codebase.Substring(codebase.IndexOf('_') + 1, codebase.Length - (codebase.IndexOf('_') + 1)).Replace('_', '.').TrimEnd('\\');
xDoc["asmv1:assembly"]["deployment"].Attributes.Append(node);
xDoc["asmv1:assembly"]["deployment"].InnerXml = "<subscription><update><beforeApplicationStartup /></update></subscription>";
}
xDoc.Save(Path.Combine(filedir.FullName, "<AppName>.application"));
p.StartInfo.Arguments = "-Sign \"<AppName>.application\" -CertFile \"<AppName>.pfx\" -Password <password>";
while (p.StartInfo.Arguments.Contains(".\\.\\"))
p.StartInfo.Arguments = p.StartInfo.Arguments.Replace(".\\.\\", ".\\");
Logger.Instance.LogInfo("Starting application: " + p.StartInfo.FileName + "\n\tWith arguments: " + p.StartInfo.Arguments, Logger.InfoType.Information);
p.Start();
while (!p.HasExited)
{
Thread.Sleep(100);
}
}
}