SoFunction
Updated on 2025-03-07

C# implements txt positioning the specified row complete instance

This article describes the method of C# to implement txt positioning specified rows. Share it for your reference. The specific implementation method is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace WfpApp
{
 class PositionNotepad
 {
 [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>  
 /// Position to 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> public static bool PositionNotePad(string strFullName, string strRow)
 {
  int iRow;
  (strRow, out iRow);
  if (iRow &lt;= 0)
  {
  return false;
  }
  // Check whether the current file is open  IntPtr hwnd = FindWindow("Notepad", ("{0} - Notebook", (strFullName)));
  if (hwnd.ToInt32() == 0)
  {
  Process p = (@"", strFullName);
  //Wait for a second, wait for the text to open, and focus to notepad  (1000);
  ("{DOWN " + (iRow - 1) + "}");
  ("{HOME}"); //The first  ("+{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 first   ("+{END}"); //Select the current line  }
  }
  return true;
 }
 }
}

Called:

string path = @"C:\Users\ZKK\Desktop\";
bool res = (path, "5");

I hope this article will be helpful to everyone's C# programming.