Writing text to an open notepad file
-
Hi guys, I need to write some formatted string into a notepad file that is already opened by the user. I tried using TextWriter , but we need to close and open the notepad file to reflect the changes. Also Process.StartInfo doesnot help. Can anyone help me out? Thanks in advance. Jrk
-
Hi guys, I need to write some formatted string into a notepad file that is already opened by the user. I tried using TextWriter , but we need to close and open the notepad file to reflect the changes. Also Process.StartInfo doesnot help. Can anyone help me out? Thanks in advance. Jrk
Hello, One solution would be to use the Process class of System.Diagnostic namespace. There you find a member called MainWindowHandle, which is an IntPtr. In combination with the "user32.dll" method "SetForegroundWindow", you can activate the open notepad instance. After that is done you could use "SendKeys.Send" method to send your text. Here is a little demo:
using System.Runtime.InteropServices; Process[] procNP = Process.GetProcessesByName("notepad"); if(procNP.Length>0) { if(procNP[0].MainWindowHandle!=IntPtr.Zero) { SetForegroundWindow(procNP[0].MainWindowHandle); System.Threading.Thread.Sleep(100); SendKeys.Send("Hello World!"); } foreach(Process actP in procNP) { actP.Dispose(); } } [DllImport("user32.dll")] private static extern int SetForegroundWindow (IntPtr hWnd);
Hope it helps!All the best, Martin
-
Hello, One solution would be to use the Process class of System.Diagnostic namespace. There you find a member called MainWindowHandle, which is an IntPtr. In combination with the "user32.dll" method "SetForegroundWindow", you can activate the open notepad instance. After that is done you could use "SendKeys.Send" method to send your text. Here is a little demo:
using System.Runtime.InteropServices; Process[] procNP = Process.GetProcessesByName("notepad"); if(procNP.Length>0) { if(procNP[0].MainWindowHandle!=IntPtr.Zero) { SetForegroundWindow(procNP[0].MainWindowHandle); System.Threading.Thread.Sleep(100); SendKeys.Send("Hello World!"); } foreach(Process actP in procNP) { actP.Dispose(); } } [DllImport("user32.dll")] private static extern int SetForegroundWindow (IntPtr hWnd);
Hope it helps!All the best, Martin
Thank you Martin. It solves my other problem. But this is different. I dont want the notepad to bring to the foreground. I want to update the openned notepad file while it is in the background. Can you help me? Thanks
-
Thank you Martin. It solves my other problem. But this is different. I dont want the notepad to bring to the foreground. I want to update the openned notepad file while it is in the background. Can you help me? Thanks
Hello,
Member 514252 wrote:
Thank you Martin
:rose:
Member 514252 wrote:
I dont want the notepad to bring to the foreground. I want to update the openned notepad file while it is in the background
OK, I understand your task and tried to find a solution, but have to admit that I didn't succeed. What have I tried: SendMessage in cmnbination with WM_SETTEXT.
[DllImport("user32.dll",EntryPoint="SendMessage", CharSet=CharSet.Auto)] private static extern int SendMessage( int hwnd, int uMsg, int wParam, System.Text.StringBuilder lParam); private const int WM_SETTEXT = 0x000c; SendMessage((int)procNP[0].MainWindowHandle,WM_SETTEXT, myText.Length, new System.Text.StringBuilder(myText));
Had the effect, that the FileNameChanged (HeadlineText of the Notepad) changed, not the text itselfe. Sorry for not really helping you. P.S.:A dirty workaround could be to reset the Foregroundwindow to your actual applicationSetForegroundWindow(procNP[0].MainWindowHandle); System.Threading.Thread.Sleep(100); SendKeys.SendWait("Hello World!"); SetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
What you have to check is if the notepad IsIconic (minimized). If yes, the SendKeys will not work.private const int SW_SHOWNORMAL = 1; private const int SW_SHOWMINIMIZED = 2; private const int SW_SHOWMAXIMIZED = 3; /// <summary> /// Gets a Flag if Window is Iconic or not /// </summary> /// <param name="hWnd">windowhandle</param> /// <returns>true or false</returns> [System.Runtime.InteropServices.DllImport("user32.dll")] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] private static extern bool IsIconic(IntPtr hWnd); /// <summary> /// Show a Window /// </summary> /// <param name="hwnd">windowhandle</param> /// <param name="nCmdShow">parameter to set a special state</param> /// <returns></returns> [DllImport("user32")] private static extern int ShowWindow(IntPtr hwnd, int nCmdShow); IntPtr handleNotePad = procNP[0].MainWindowHandle; if(handleNotePad!=IntPtr.Zero) { bool wasIconic = IsIconic(handleNotePad); if(wasIconic) ShowWindow(handleNotePad, SW_SHOWNORMAL); SetForegroundWindow(handleNotePad);
-
Hello,
Member 514252 wrote:
Thank you Martin
:rose:
Member 514252 wrote:
I dont want the notepad to bring to the foreground. I want to update the openned notepad file while it is in the background
OK, I understand your task and tried to find a solution, but have to admit that I didn't succeed. What have I tried: SendMessage in cmnbination with WM_SETTEXT.
[DllImport("user32.dll",EntryPoint="SendMessage", CharSet=CharSet.Auto)] private static extern int SendMessage( int hwnd, int uMsg, int wParam, System.Text.StringBuilder lParam); private const int WM_SETTEXT = 0x000c; SendMessage((int)procNP[0].MainWindowHandle,WM_SETTEXT, myText.Length, new System.Text.StringBuilder(myText));
Had the effect, that the FileNameChanged (HeadlineText of the Notepad) changed, not the text itselfe. Sorry for not really helping you. P.S.:A dirty workaround could be to reset the Foregroundwindow to your actual applicationSetForegroundWindow(procNP[0].MainWindowHandle); System.Threading.Thread.Sleep(100); SendKeys.SendWait("Hello World!"); SetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
What you have to check is if the notepad IsIconic (minimized). If yes, the SendKeys will not work.private const int SW_SHOWNORMAL = 1; private const int SW_SHOWMINIMIZED = 2; private const int SW_SHOWMAXIMIZED = 3; /// <summary> /// Gets a Flag if Window is Iconic or not /// </summary> /// <param name="hWnd">windowhandle</param> /// <returns>true or false</returns> [System.Runtime.InteropServices.DllImport("user32.dll")] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] private static extern bool IsIconic(IntPtr hWnd); /// <summary> /// Show a Window /// </summary> /// <param name="hwnd">windowhandle</param> /// <param name="nCmdShow">parameter to set a special state</param> /// <returns></returns> [DllImport("user32")] private static extern int ShowWindow(IntPtr hwnd, int nCmdShow); IntPtr handleNotePad = procNP[0].MainWindowHandle; if(handleNotePad!=IntPtr.Zero) { bool wasIconic = IsIconic(handleNotePad); if(wasIconic) ShowWindow(handleNotePad, SW_SHOWNORMAL); SetForegroundWindow(handleNotePad);
Thank you again for your effort Martin. As you said, i tried SendMessage-ing WM_CHAR, it works for Notepad and Wordpad. But i couldnot write into Excel or Word file. With SendKeys, I am able to send text to any window. Is there anyway we can do to avoid showing the file? JRK