2015-11-07 29 views
1

我一直在嘗試與我的同事一起弄清楚這一點。
我使用Ngen製作原生圖像並提升客戶端上的應用程序性能。
讓我們假設的exe文件名是Example.exe
我嘗試在應用程序做的是,我NGEN安裝example.exe和NGEN將安裝本機映像的assebmlies
但這裏的問題是:
如果我嘗試提高CMD並運行
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen Display example.exe
example.exe文件名和原生圖像的版本顯示,但是,使用下面的代碼在Windows窗體c#中做同樣的事情,它不會顯示example.exe文件的文件名和版本,並顯示相關性,例如Telerik等。
所以我的問題是爲什麼CMD返回正確的結果,而進程返回依賴關係,而不是文件名和版本本身?
這裏是我的一些代碼:
NGEN顯示返回當在C#winforms中使用Process.Start()運行時,提升CMD的結果不同於此結果

public static bool Run() 
     { 

      var f = new Loading(); 

      bool state = false; 

      var b = new BackgroundWorker(); 

      string runtimeStr = RuntimeEnvironment.GetRuntimeDirectory(); 
      string ngenStr = Path.Combine(runtimeStr, "ngen"); 

      Process process = new Process 
      { 
       StartInfo = 
       { 
        FileName = ngenStr, 
        WindowStyle = ProcessWindowStyle.Hidden, 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        RedirectStandardError = true, 
        CreateNoWindow = false 
       } 
      }; 

      if (Environment.OSVersion.Version.Major >= 6) 
      { 
       process.StartInfo.Verb = "runas"; 
      } 
      process.StartInfo.Arguments = "display " + Application.ProductName; 

      process.Start(); 
      // process.WaitForExit(); 

      string stdoutx = process.StandardOutput.ReadToEnd(); 
       process.WaitForExit(); 
} 


但是stdoutx串不包含example.exe文件名或版本
編輯:什麼的應該是什麼樣的例子是:
這是什麼CMD的回報這是正確的結果:

Example, Version=1.0.0.19, Culture=neutral, PublicKeyToken=null <debug> 


這是什麼:

Microsoft (R) CLR Native Image Generator - Version 4.6.79.0 
Copyright (c) Microsoft Corporation. All rights reserved. 

NGEN Roots: 

C:\Project\Example\bin\Debug\Example.EXE 

NGEN Roots that depend on "Example": 

C:\Project\Example\bin\Debug\Example.EXE 

Native Images: 

Klik.Windows.Forms.EntryLib.V2.2008, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null 
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
NCalc, Version=1.3.8.0, Culture=neutral, PublicKeyToken=973cde3f1cafed03 
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed 
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed 
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed 
RedGate.Migrations.Core, Version=1.0.0.1401, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 <debug> 
RedGate.Shared.ComparisonInterfaces, Version=1.0.2.278, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
RedGate.Shared.SQL, Version=9.9.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
RedGate.Shared.Utils, Version=9.9.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
RedGate.SOCCompareInterface, Version=3.0.50.59, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
RedGate.SQLCompare.ASTParser, Version=11.0.0.414, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
RedGate.SQLCompare.Engine, Version=11.1.0.2, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 <debug> 
RedGate.SQLCompare.Rewriter, Version=11.1.0.2, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 
System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Data.SQLite, Version=1.0.85.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139 
System.Deployment, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
Telerik.Reporting, Version=9.0.15.324, Culture=neutral, PublicKeyToken=a9d7983dfcc261be 
Telerik.ReportViewer.WinForms, Version=9.0.15.324, Culture=neutral, PublicKeyToken=a9d7983dfcc261be 
Telerik.WinControls.ChartView, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e 
Telerik.WinControls, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e 
Telerik.WinControls.GridView, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e 
Telerik.WinControls.Themes.Office2010Black, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e 
Telerik.WinControls.Themes.Office2010Silver, Version=2015.1.331.40, Culture=neutral, PublicKeyToken=5bb2a467cbec794e 

回答

2

是的,你不這樣做正確。您正在使用該命令的語法是:

ngen.exe display [assemblyName | assemblyPath] 

的AssemblyNameassemblyPath之間的差異。當你從cmd.exe運行它時,你鍵入「example」。並因此指定了(部分)程序集名稱。在你的代碼中,你使用了「example.exe」。並且Environment.CurrentDirectory是正確設置的,這是一個常見的事故。所以Ngen.exe可以找到文件,它會變得很健談。不僅列出程序集名稱,還列出其所有依賴項的本機映像。像Klik.Windows.Forms.EntryLib等等。

不是唯一的問題,注意你指定了部分程序集名稱。你想確保你的裝配版的版本是正版的。使代碼看起來像這個:

process.StartInfo.Arguments = "display \"" + 
     System.Reflection.Assembly.GetEntryAssembly().FullName + "\""; 

,與項目>屬性>調試選項卡>勾去掉「啓用在Visual Studio宿主進程」複選框測試。

煩惱的次要事情是原生圖像(x86與x64)的差距,並讓您的應用程序升高,這樣您就可以安裝原生圖像,通常只有在您的應用程序安裝時纔會拍攝。

相關問題