SoFunction
Updated on 2025-03-07

Small example of C# method to locate txt specified rows


            [DllImport("", EntryPoint = "FindWindow")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("")]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            [DllImport("")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            ///<summary>
///Locate the specified line of the txt file
            ///</summary>
///<param name="strFullName">File path</param>
///<param name="strRow">Specify row</param>
///<returns>Whether the positioning is successful</returns>
            private bool LocateNotePad(string strFullName, string strRow)
            {
                int iRow;
                (strRow, out iRow);
                if (iRow <= 0)
                {
                    return false;
                }
IntPtr hwnd = FindWindow("Notepad", ("{0} - Notepad", (strFullName)));// Check whether the current file is open
                if (hwnd.ToInt32() == 0)
                {
                    Process p = (@"",strFullName);
(1000); //Wait for a second, wait for the text to open, and focus to notepad
                    ("{DOWN " + (iRow - 1) + "}");
("{HOME}"); //The beginning of the line
("+{END}"); //Select the current line
                    return true;
                }
                else
                {
                    hwnd = FindWindowEx(hwnd, , "Edit", );
                    if (hwnd.ToInt32() == 0) return false;
                    else
                    {
                        SetForegroundWindow(hwnd);
("^{HOME}");//Position the cursor to the first line
                        ("{DOWN " + (iRow - 1) + "}"); //
("{HOME}"); //The beginning of the line
("+{END}"); //Select the current line
                    }
                }
                return true;
            }