none
C#运行exe运行不了 RRS feed

  • 常规讨论

  • static string RunFile(string file)
            {
                Process p = new Process();
                if (p == null)
                {
                    Console.WriteLine("Create process error.");
                    return null;
                }
                p.StartInfo.FileName = file;

                p.Start();

                return null;
            }

    如果运行cmd.exe可以运行,为什么我用vs编译出的一个exe就不能运行呢?

    谢谢

                
    2013年7月17日 9:49

全部回复

  • 我也遇到过您这种情况,当时是想把你这个方法写到服务的OnStart方法中,死活就是不成功,无奈之下换了如下的一种方法:

    #region P/Invoke WTS APIs /// <summary> /// Struct, Enum and P/Invoke Declarations of WTS APIs. /// </summary> /// private const int WTS_CURRENT_SERVER_HANDLE = 0; private enum WTS_CONNECTSTATE_CLASS { WTSActive, WTSConnected, WTSConnectQuery, WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown, WTSInit } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct WTS_SESSION_INFO { public UInt32 SessionID; public string pWinStationName; public WTS_CONNECTSTATE_CLASS State; } [DllImport("WTSAPI32.DLL", SetLastError = true, CharSet = CharSet.Auto)] static extern bool WTSEnumerateSessions( IntPtr hServer, [MarshalAs(UnmanagedType.U4)] UInt32 Reserved, [MarshalAs(UnmanagedType.U4)] UInt32 Version, ref IntPtr ppSessionInfo, [MarshalAs(UnmanagedType.U4)] ref UInt32 pSessionInfoCount ); [DllImport("WTSAPI32.DLL", SetLastError = true, CharSet = CharSet.Auto)] static extern void WTSFreeMemory(IntPtr pMemory); [DllImport("WTSAPI32.DLL", SetLastError = true, CharSet = CharSet.Auto)] static extern bool WTSQueryUserToken(UInt32 sessionId, out IntPtr Token); #endregion #region P/Invoke CreateProcessAsUser /// <summary> /// Struct, Enum and P/Invoke Declarations for CreateProcessAsUser. /// </summary> /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct STARTUPINFO { public Int32 cb; public string lpReserved; public string lpDesktop; public string lpTitle; public Int32 dwX; public Int32 dwY; public Int32 dwXSize; public Int32 dwYSize; public Int32 dwXCountChars; public Int32 dwYCountChars; public Int32 dwFillAttribute; public Int32 dwFlags; public Int16 wShowWindow; public Int16 cbReserved2; public IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public int dwProcessId; public int dwThreadId; } [DllImport("ADVAPI32.DLL", SetLastError = true, CharSet = CharSet.Auto)] static extern bool CreateProcessAsUser( IntPtr hToken, string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, string lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation ); [DllImport("KERNEL32.DLL", SetLastError = true, CharSet = CharSet.Auto)] static extern bool CloseHandle(IntPtr hHandle); #endregion // 启动程序的方法 void RunProc() { IntPtr ppSessionInfo = IntPtr.Zero; UInt32 SessionCount = 0; if (WTSEnumerateSessions( (IntPtr)WTS_CURRENT_SERVER_HANDLE, // Current RD Session Host Server handle would be zero. 0, // This reserved parameter must be zero. 1, // The version of the enumeration request must be 1. ref ppSessionInfo, // This would point to an array of session info. ref SessionCount // This would indicate the length of the above array. )) { for (int nCount = 0; nCount < SessionCount; nCount++) { // Extract each session info and check if it is the // "Active Session" of the current logged-on user. WTS_SESSION_INFO tSessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure( ppSessionInfo + nCount * Marshal.SizeOf(typeof(WTS_SESSION_INFO)), typeof(WTS_SESSION_INFO) ); if (WTS_CONNECTSTATE_CLASS.WTSActive == tSessionInfo.State) { IntPtr tokenH = IntPtr.Zero; if (WTSQueryUserToken(tSessionInfo.SessionID, out tokenH)) { Console.WriteLine("tokenH " + tokenH.ToString()); STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION tProcessInfo; si.cb = Marshal.SizeOf(si); //StartAppPath为你要调用的exe文件的路径 CreateProcessAsUser(tokenH, StartAppPath, @" D:\temp\test.txt", IntPtr.Zero, IntPtr.Zero, false, 0, null, null, ref si, out tProcessInfo); Console.WriteLine(Marshal.GetLastWin32Error()); Console.WriteLine("PROCESS_INFORMATION " + tProcessInfo.dwProcessId.ToString()); } } } } WTSFreeMemory(ppSessionInfo); }

    如果您找到了更好的解决方法欢迎分享。


    2013年7月17日 12:22
  • 谢谢,我尝试一下
    2013年7月18日 2:07
  • 我试了下,还是不行,直接用vs的命令行编译/runexit了
    2013年7月18日 3:16