[英]VS2010 on Win7, installer class within a setup is not called
它是 setupproject 中的一個安裝程序類,它位於 winform 項目中。 直到現在我沒有任何錯誤消息,只是沒有被調用。 RunInstallerAttribute 設置為 true。
唯一剩下的是“主要空白”,但我不能說,因為 winform 項目需要它。
這是整個代碼:
using System;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;
using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;
using System.Windows.Forms;
using System.Text;
using System.Threading;
[RunInstaller(true)]
partial class MyInstaller : Installer
{
public MyInstaller()
{
MessageBox.Show("MyInstaller");
InitializeComponent();
}
#region "onAfter"
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
}
protected override void OnAfterRollback(IDictionary savedState)
{
base.OnAfterRollback(savedState);
}
protected override void OnAfterUninstall(IDictionary savedState)
{
base.OnAfterUninstall(savedState);
}
#endregion
#region "OnBefore"
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
}
protected override void OnBeforeRollback(IDictionary savedState)
{
base.OnBeforeRollback(savedState);
}
protected override void OnBeforeUninstall(IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
}
#endregion
#region "OnCommitt"
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
}
protected override void OnCommitting(IDictionary savedState)
{
base.OnCommitting(savedState);
}
#endregion
#region "Rollback"
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
try
{
string fileName = savedState["myExe"].ToString();
//MsgBox("Rollback ..." & fileName)
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
catch (InstallException ex)
{
MessageBox.Show("Uninstall" + ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Uninstall" + ex.ToString());
}
}
#endregion
#region "Uninstall"
public override void Uninstall(IDictionary savedState)
{
try
{
string fileName = savedState["myExe"].ToString();
if (File.Exists(fileName))
{
File.Delete(fileName);
}
base.Uninstall(savedState);
}
catch (InstallException ex)
{
MessageBox.Show("Uninstall" + ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Uninstall" + ex.ToString());
}
}
#endregion
#region "Install"
public override void Install(IDictionary savedState)
{
MessageBox.Show("Install ");
string strTargetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "myTest");
savedState.Add("myExe", strTargetPath);
base.Install(savedState);
}
#endregion
#region "Commit"
public override void Commit(IDictionary savedState)
{
string strPath = "";
try
{
strPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
strPath = Path.Combine(strPath, "myTest\\myApp.exe");
using (Process process = new Process())
{
process.StartInfo.FileName = "myApp.exe";
process.StartInfo.Arguments = strPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (process.WaitForExit(1000) &&
outputWaitHandle.WaitOne(1000) &&
errorWaitHandle.WaitOne(1000))
{
// Process completed. Check process.ExitCode here.
}
else
{
// Timed out.
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Commit " + ex.ToString());
Application.Exit();
}
}
#endregion
}
最可能的原因是您沒有在安裝項目中將程序集添加為自定義操作,因為您沒有提到這樣做。
它現在有效。 我創建了那些“CommonAppDataFolder”,在 ac# 項目中似乎需要它。 幾年前我用 vb.net proejct 制作它,不記得需要它。 它需要安裝應用程序,當然具有這些特殊權限,但安裝程序的調用並不依賴於它。
最后,我必須在“Commit”中取出使用部分“using (Process process = new Process())”。 因為無論我對 -filename- 和 -argument- 設置什么,它總是會拋出一個錯誤, - 它無法找到文件夾“C/Program86/....myApp.exe”。
當我發現這個使用部分時,我真的很高興,以為我現在還剩下什么來關閉最終的安裝窗口“安裝完成”。 我總是不得不手動關閉這個窗口。
Commit 過程現在看起來像這樣:
public override void Commit(IDictionary savedState)
{
string strPath = "";
var myProcess = new Process();
try
{
base.Commit(savedState);
strPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Test");
strPath = Path.Combine(strPath, "myApp.exe");
myProcess.StartInfo.FileName = strPath;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
myProcess.WaitForExit(500);
myProcess.Close();
myProcess = null;
}
catch (Exception ex)
{
MessageBox.Show("public override void Commit " + ex.ToString());
Application.Exit();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.