在.Net 4框架中编译的应用程序将不会在Server 2012上运行

本文关键字:Server 2012 运行 应用程序 Net 框架 编译 | 更新日期: 2023-09-27 18:20:50

我编译应用程序以使用.NET Framework 4,因为我需要它来在不支持.NET Framework 4.5的XP和Server 2003上工作。问题是它现在在XP和2003服务器上运行良好,但在我的Server 2012上运行不好。Server 2012有.NET 4.5,据我所知,.NET 4.5与.NET 4 向后兼容

下面是我在2012服务器上收到的错误:


应用程序中出现未处理的异常。如果单击"继续",应用程序将忽略此错误并尝试继续。如果单击"退出",应用程序将立即关闭。

系统找不到指定的文件。

详细信息:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at MyApplication.Form1.DirectoryExporter_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18449 built by: FX451RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
MyApplication
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/administrator.DM/Desktop/MyApplication.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

以下是此时调用的代码:

public string get_regValue(string userroot, string subkey, string keyname, string app)
        {
            string userRoot = userroot;
            string subKey = subkey;
            string keyName = userRoot + "''" + subKey;
            string regValue = (string)Registry.GetValue(keyName, keyname, null);
            regValue = regValue + app;
            return regValue;
        }
        private void DirectoryExporter_Click(object sender, EventArgs e)
        {
            // Button to Launch the Directory Exporter
            string regKey;
            RegistryKey openKey = Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE''SOFTWARE''Wow6432Node''Dell");
            // Checks for key, if null (empty) get 32 bit key
            if (openKey == null)
            {
                // 32 bit key
                regKey = "HKEY_LOCAL_MACHINE''SOFTWARE''Dell";
            }
            else
            {
                // 64 bit key
                regKey = "HKEY_LOCAL_MACHINE''SOFTWARE''Wow6432Node''Dell";
            }
            var regvalue = get_regValue(regKey, "Migrator for GroupWise", "AdminInstallDir", "gwdirapp.exe");
            Process.Start(@regvalue);
        }

在.Net 4框架中编译的应用程序将不会在Server 2012上运行

我强烈怀疑这与您使用的.NET版本无关。毕竟,从堆栈跟踪中,您可以清楚地看到您的代码正在执行:

at MyApplication.Form1.DirectoryExporter_Click(Object sender, EventArgs e)

我怀疑这个问题是权限问题——在Windows Server 2012下,由于它在一个更加锁定的环境中运行,无论你试图做什么都会失败。

或者,可能是您试图启动一个存在于开发机器上但不存在于服务器上的进程。如果不看代码,我们真的无法判断,但您基本上应该仔细查看您在DirectoryExporter_Click中所做的操作,尤其是当您调用Process.Start时。检查您试图运行的可执行文件,然后检查它是否存在于服务器上,以及您运行的用户是否有执行它的权限。

我通过编辑代码解决了这个问题,如下所示:

public string get_regValue(string userroot, string subkey, string keyname, string app)
{
    string userRoot = userroot;
    string subKey = subkey;
    string keyName = userRoot + "''" + subKey;
    string regValue = (string)Registry.GetValue(keyName, keyname, null);
    regValue = regValue + app;
    return regValue;
}
private void DirectoryExporter_Click(object sender, EventArgs e)
{
    // Button to Launch the Directory Exporter
    string regKey;
    RegistryKey openKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE''Wow6432Node''Dell");
    // Checks for key, if null (empty) get 32 bit key
    if (openKey == null)
    {
        // 32 bit key
        regKey = "HKEY_LOCAL_MACHINE''SOFTWARE''Dell";
    }
    else
    {
        // 64 bit key
        regKey = "HKEY_LOCAL_MACHINE''SOFTWARE''Wow6432Node''Dell";                
    }
    var regvalue = get_regValue(regKey, "Migrator for GroupWise", "AdminInstallDir", "gwdirapp.exe");
    Process.Start(regvalue);