WinCE下使用C#来打开一个外部文件
2009-11-16 22:26
597 查看
使用Windows的开发机上用C#启动一个外部程序的方法有很多,但这些方法用在使用WinCE的目标工控机上都无能为力,现在以打开一个IE为例,介绍如何在WinCE下使用C#来打开一个外部文件:首先添加命名空间 using System.Runtime.InteropServices;,然后调用API函数: [DllImport("coredll.Dll", EntryPoint="CreateProcess", SetLastError=true)] extern static int CreateProcess(string strImageName, string strCmdLine, IntPtr pProcessAttributes, IntPtr pThreadAttributes , int bInheritsHandle, int dwCreationFlags, IntPtr pEnvironment, IntPtr pCurrentDir, IntPtr bArray, ProcessInfo oProc); public class ProcessInfo { public Int32 hProcess; public Int32 hThread; public Int32 ProcessID; public Int32 ThreadID; } 最后就可以编写你需要打开IE的代码了(点击一个按钮打开IE浏览器中相应内容,此例程要求打开目标工控机硬盘上的Readme文件): private void button_Click(object sender, System.EventArgs e) { ProcessInfo pi = new ProcessInfo(); CreateProcess("//windows//iesample.exe" , "//HardDisk//Readme.htm", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi); } 2、有时候我们会希望我们的程式只被执行一次,VB的时代我们会用App.PrevInstance,而.net的时代我们可以用下列方式实现 [STAThread] static void Main() { //如果跟本程式命名的行程只有一个才执行程式 if (System.Diagnostics.Process.GetProcessesByName( Application.ProductName).Length == 1) { Application.Run(new Form1()); } } 但此方法在WinCE下无法实现,所以我们还是要先调用动态链接库, [DllImport("coredll.Dll")] private static extern int GetLastError(); [DllImport("coredll.Dll")] private static extern int ReleaseMutex(IntPtr hMutex); [DllImport("coredll.Dll")] private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName); [StructLayout( LayoutKind.Sequential)] public class SECURITY_ATTRIBUTES { public int nLength; public int lpSecurityDescriptor; public int bInheritHandle; } const int ERROR_ALREADY_EXISTS = 0183; 然后编写代码 static void Main() { #region Api_Call CreateMutex; IntPtr hMutex; hMutex=CreateMutex(null,false,"程序名"); if (GetLastError()!=ERROR_ALREADY_EXISTS) { Application.Run(new Frmmenu()); } else { MessageBox.Show("本程序只允许同时运行一个"); ReleaseMutex(hMutex); } #endregion } 3、有时我们希望自己的程序能做到激活一个窗体,但在 .NET Framework 中没有函数可以激活属于另外一个进程或程序的窗体。所以,我们还是要调用API函数来实现: using System.Runtime.InteropServices; [DllImport("coredll.Dll")] public static extern IntPtr FindWindow(String classname, String title); [DllImport("coredll.Dll")] public static extern void SetForegroundWindow(IntPtr hwnd); 然后使用下列代码即可 IntPtr hDlg; hDlg=FindWindow(null, "窗口标题"); SetForegroundWindow( hDlg ); 最后,WinCE下的C#里不支持GroupBox控件,建议使用Panel控件代替;不支持Frame控件,如果非要达到那样的效果,可以用Label和TextBox组和起来应付一下。 其实,任何时候,只要.NET Framework无法满足编程者的需要,通常都可以使用托管(interop)机制直接与Windows 交互。大家也许看出调用原有的[DllImport("user32 .Dll")]动态链接库时无法满足WinCE下程序要求,所以我们调用了[DllImport ("coredll.Dll")],并不是小嫚儿自己想到去调用它的,而是在网上看到了一篇“bitsbird”写的例程,很多人将自己的经验无私的与别人分享,所以小嫚儿写了此篇文章,虽然让行家看起来文章很浅显,但希望能给象小嫚儿这样的初学者提供一些捷径。
相关文章推荐
- WinCE下使用C#来打开一个外部文件:
- 在WinCE下使用C#来打开一个外部程序
- wince下使用C#打开一个外部程序
- 在WinCE下使用C#来打开一个外部程序
- wince下使用C#打开一个外部程序
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri
- [导入]用c#打开一个外部文件
- 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议
- 用C#打开一个pdf文件
- Linux 文件系统剖析: 使用 /proc 文件系统来访问 Linux 内核的内容 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口
- 问题:C#打开一个文本文档往里面写数据,没有就新建文档 ;结果:c#FileStream文件读写(转)
- 夺命雷公狗C/C++-----8---使用ShellExecute打开一个文件和一个网址和打印文件
- 如果使用追加标志O_APPEND打开一个文件以便读、写,能否仍能用lseek在任一位置开始读? 能否用lseek更新文件中任一部分的数据?
- 使用C#选择文件夹、打开文件夹、选择文件
- 使用C#选择文件夹、打开文件夹、选择文件或者如何使用C#选择文件夹
- C#初学者--建立一个button打开并显示dbf文件
- C#学习笔记(十六):使用打开保存文件对话框