C# WIN10x64位 编译文件为32位程序时候 访问C盘报错
var file = "C:\\Windows\\System32\\osk.exe"; Process.Start(file);
报错 找不到文件
使用 win32API 也打不开 可以打Calc.exe
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Web; using System.Windows.Forms; namespace WindowsForms打开exe { static class Program { [DllImport("kernel32.dll")] public static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation ); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { var file = "C:\\Windows\\System32\\osk.exe"; // Calc.exe STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); //可以打开 CreateProcess("C:\\WINDOWS\\SYSTEM32\\Calc.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi); Console.ReadLine(); //StartupNotepad(); //string argm = "C:\\Windows\\System32\\osk.exe"; //STARTUPINFO sInfo = new STARTUPINFO(); //PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); //if (!CreateProcess(null, new StringBuilder(argm), null, null, false, 0, null, null, ref sInfo, ref pInfo)) //{ // throw new Exception("调用失败"); //} //uint i = 0; //WaitForSingleObject(pInfo.hProcess, int.MaxValue); //GetExitCodeProcess(pInfo.hProcess, ref i); //CloseHandle(pInfo.hProcess); //CloseHandle(pInfo.hThread); //StartupNotepad(); //dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe"; //file = "C:\\Windows\\System32\\osk.exe"; ////判断文件是否存在 //if (System.IO.File.Exists(file)) //{ // Process.Start(file); //} } #region Win32 Api : WaitForSingleObject //检测一个系统核心对象(线程,事件,信号)的信号状态,当对象执行时间超过dwMilliseconds就返回,否则就一直等待对象返回信号 [DllImport("Kernel32.dll")] public static extern uint WaitForSingleObject(System.IntPtr hHandle, uint dwMilliseconds); #endregion #region Win32 Api : CloseHandle //关闭一个内核对象,释放对象占有的系统资源。其中包括文件、文件映射、进程、线程、安全和同步对象等 [DllImport("Kernel32.dll")] public static extern bool CloseHandle(System.IntPtr hObject); #endregion #region Win32 Api : GetExitCodeProcess //获取一个已中断进程的退出代码,非零表示成功,零表示失败。 //参数hProcess,想获取退出代码的一个进程的句柄,参数lpExitCode,用于装载进程退出代码的一个长整数变量。 [DllImport("Kernel32.dll")] static extern bool GetExitCodeProcess(System.IntPtr hProcess, ref uint lpExitCode); #endregion [DllImport("Kernel32.dll", CharSet = CharSet.Ansi)] public static extern bool CreateProcess( StringBuilder lpApplicationName, StringBuilder lpCommandLine, SECURITY_ATTRIBUTES lpProcessAttributes, SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, StringBuilder lpEnvironment, StringBuilder lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation ); //声明结构体 [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] public class SECURITY_ATTRIBUTES { public int nLength; public string lpSecurityDescriptor; public bool bInheritHandle; } [StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public int dwProcessId; public int dwThreadId; } [StructLayout(LayoutKind.Sequential)] public struct STARTUPINFO { public int cb; public string lpReserved; public string lpDesktop; public int lpTitle; public int dwX; public int dwY; public int dwXSize; public int dwYSize; public int dwXCountChars; public int dwYCountChars; public int dwFillAttribute; public int dwFlags; public int wShowWindow; public int cbReserved2; public byte lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); public static void StartupNotepad() { const uint NORMAL_PRIORITY_CLASS = 0x0020; bool retValue; string Application = Environment.GetEnvironmentVariable("windir") + @"\Notepad.exe"; string CommandLine = null;// @" c:\boot.ini"; PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); STARTUPINFO sInfo = new STARTUPINFO(); SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES(); SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES(); pSec.nLength = Marshal.SizeOf(pSec); tSec.nLength = Marshal.SizeOf(tSec); //Open Notepad retValue = CreateProcess( Application, CommandLine, ref pSec, ref tSec, false, NORMAL_PRIORITY_CLASS, IntPtr.Zero, null, ref sInfo, out pInfo); //Console.WriteLine("Process ID (PID): " + pInfo.dwProcessId); //Console.WriteLine("Process Handle : " + pInfo.hProcess); } } }