所有你需要有bootstrapper.exe作爲項目的一部分,首先(添加 - >現有項 - >瀏覽到bootstrapper.exe &的.config包括兩者)。對於這些文件的屬性,您必須將「生成操作」設置爲「無」和「複製到輸出目錄」爲「始終複製」。
現在你可以使用下面的代碼來運行引導程序(我那樣做,它的工作原理):
internal void SomethingWithBootStrapper()
{
//
Trace.TraceInformation("Trying to install agent...");
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "bootstrapper.exe");
psi.Arguments = @"-get http://some_url_to_download_something.com/path/to/file.zip -lr $lr(YourLocalResourceKeyHere) -unzip $lr(YourLocalResourceKeyHere)\extract -run $lr(YourLocalResourceKeyHere)\extract\Setup.exe -args /some /args /for_the_setup.exe -block";
Trace.WriteLine("Calling " + psi.FileName + " " + psi.Arguments + " ...");
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = false;
psi.RedirectStandardError = true;
// run elevated
psi.Verb = "runas";
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(psi))
{
exeProcess.PriorityClass = ProcessPriorityClass.High;
string outString = string.Empty;
// use ansynchronous reading for at least one of the streams
// to avoid deadlock
exeProcess.OutputDataReceived += (s, e) =>
{
outString += e.Data;
};
exeProcess.BeginOutputReadLine();
// now read the StandardError stream to the end
// this will cause our main thread to wait for the
// stream to close (which is when ffmpeg quits)
string errString = exeProcess.StandardError.ReadToEnd();
Trace.WriteLine(outString);
Trace.TraceError(errString);
this._isInitialized = true;
}
}
catch (Exception e)
{
Trace.TraceError(e.Message);
this._isInitialized = false;
}
}
請注意,這是100%的測試,工作代碼!
謝謝,你的建議提醒我,我忘記了「BootStrapper.exe.config」文件。因爲它在本地IIS中工作,所以我從來沒有想過這個文件:( –
無論如何,只是把這個文件放在Azure上,一切正常 –
@astaykov - 出於好奇,爲什麼你不把這個命令放在bat文件中並且調用從一個啓動任務?這是我建立引導程序的初始用例。 – dunnry