C#打開記事本實現(xiàn)實例解析
作者:qcdn
C#打開記事本的實現(xiàn)需要使用哪些方法呢?C#打開記事本的實現(xiàn)步驟是什么呢?那么本文就向你詳細(xì)介紹C#打開記事本實現(xiàn)的整個過程。
C#打開記事本的功能實現(xiàn)是如何的呢?我們在項目中也許會碰到這個小插曲,那么具體的實現(xiàn)方法是什么呢?,還有要實現(xiàn)給記事本添加內(nèi)容,那么下面我們通過實例向你介紹具體的實現(xiàn)過程。
C#打開記事本實現(xiàn)實例:
- /// ﹤summary﹥
- /// C#打開記事本之傳遞消息給記事本
- /// ﹤/summary﹥
- /// ﹤param name="hWnd"﹥﹤/param﹥
- /// ﹤param name="Msg"﹥﹤/param﹥
- /// ﹤param name="wParam"﹥﹤/param﹥
- /// ﹤param name="lParam"﹥﹤/param﹥
- /// ﹤returns﹥﹤/returns﹥
- [DllImport("User32.DLL")]
- public static extern int SendMessage(
- IntPtr hWnd, uint Msg, int wParam, string lParam);
- /// ﹤summary﹥
- /// C#打開記事本之查找句柄
- /// ﹤/summary﹥
- /// ﹤param name="hwndParent"﹥﹤/param﹥
- /// ﹤param name="hwndChildAfter"﹥﹤/param﹥
- /// ﹤param name="lpszClass"﹥﹤/param﹥
- /// ﹤param name="lpszWindow"﹥﹤/param﹥
- /// ﹤returns﹥﹤/returns﹥
- [DllImport("User32.DLL")]
- public static extern IntPtr FindWindowEx(
- IntPtr hwndParent, IntPtr hwndChildAfter,
- string lpszClass, string lpszWindow);
- /// ﹤summary﹥
- /// C#打開記事本之記事本需要的常量
- /// ﹤/summary﹥
- public const uint WM_SETTEXT = 0x000C;
- #endregion
- private void button1_Click(object sender, EventArgs e)
- {
- #region [ 啟動記事本 ]
- System.Diagnostics.Process Proc;
- try
- {
- // 啟動記事本
- Proc = new System.Diagnostics.Process();
- Proc.StartInfo.FileName = "notepad.exe";
- Proc.StartInfo.UseShellExecute = false;
- Proc.StartInfo.RedirectStandardInput = true;
- Proc.StartInfo.RedirectStandardOutput = true;
- Proc.Start();
- }
- catch
- {
- Proc = null;
- }
- #endregion
- #region [ 傳遞數(shù)據(jù)給記事本 ]
- if (Proc != null)
- {
- // C#打開記事本之調(diào)用 API, 傳遞數(shù)據(jù)
- while (Proc.MainWindowHandle == IntPtr.Zero)
- {
- Proc.Refresh();
- }
- IntPtr vHandle = FindWindowEx(
- Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
- // C#打開記事本之傳遞數(shù)據(jù)給記事本
- SendMessage(vHandle, WM_SETTEXT, 0, "Message");
- }
- #endregion
- }
C#打開記事本的具體實現(xiàn)內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#打開記事本的開發(fā)實現(xiàn)有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
博客園