在C#winforms

时间:2015-11-07 12:23:32

标签: c# .net windows winforms ngen

我一直试图和我的同事一起解决这个问题 我使用Ngen制作原生图像并提高我在客户端的应用程序性能。
假设Exe文件名是Example.exe
我在应用程序中尝试做的是我做ngen安装example.exe,并且ngen将为assebmlies安装本机图像 不过这是问题:
如果我尝试使用Elevated CMD并运行C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen Display example.exe
显示example.exe文件名和本机映像的版本,但是,使用以下代码在Windows窗体c#中执行相同操作,它不显示example.exe文件的文件名和版本,并显示相反,例如Telerik等 所以我的问题是为什么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

1 个答案:

答案 0 :(得分:2)

是的,你没有正确地这样做。您正在使用的命令的语法是:

 ngen.exe display [assemblyName | assemblyPath]

请注意 assemblyName assemblyPath 之间的区别。当您从cmd.exe运行它时,键入&#34;示例&#34;。并因此指定(部分)程序集名称。在您的代码中,您使用了&#34; example.exe&#34;。并且Environment.CurrentDirectory是偶然设置的,这是一个常见的事故。所以Ngen.exe可以找到文件并且它很健谈。不仅列出程序集名称,还列出其所有依赖项的本机映像。像Klik.Windows.Forms.EntryLib等。

不是唯一的问题,请注意您指定了部分程序集名称。您希望确保程序集的特定版本是否已被添加。使代码看起来像这样:

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

使用Project&gt;进行测试属性&gt;调试选项卡&gt;取消&#34;启用Visual Studio托管过程&#34;复选框。

要担心的次要问题是本机映像(x86 vs x64)的位数以及提升您的应用程序以便安装本机映像,这些内容通常只有在您的应用程序安装时才有效。