Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Writing text to an open notepad file

Writing text to an open notepad file

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 513823
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • U User 513823

      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

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      U 1 Reply Last reply
      0
      • M Martin 0

        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

        U Offline
        U Offline
        User 513823
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • U User 513823

          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

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          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 application SetForegroundWindow(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);

          U 1 Reply Last reply
          0
          • M Martin 0

            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 application SetForegroundWindow(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);

            U Offline
            U Offline
            User 513823
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups