国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實(shí)現(xiàn)txt定位指定行完整實(shí)例

2020-01-24 01:31:12
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(xiàn)txt定位指定行的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.IO;using System.Runtime.InteropServices;namespace WfpApp{ class PositionNotepad { [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); ///<summary>   /// 定位到txt文件指定行   ///</summary>   ///<param name="strFullName">文件路徑</param>   ///<param name="strRow">指定行</param>   ///<returns>定位是否成功</returns>   public static bool PositionNotePad(string strFullName, string strRow) {  int iRow;  int.TryParse(strRow, out iRow);  if (iRow <= 0)  {  return false;  }  //查看當(dāng)前文件是否已打開   IntPtr hwnd = FindWindow("Notepad", string.Format("{0} - 記事本", Path.GetFileName(strFullName)));  if (hwnd.ToInt32() == 0)  {  Process p = Process.Start(@"notepad.exe", strFullName);  //等一秒,等文本打開,焦點(diǎn)去到notepad  p.WaitForInputIdle(1000);  System.Windows.Forms.SendKeys.SendWait("{DOWN " + (iRow - 1) + "}");  System.Windows.Forms.SendKeys.SendWait("{HOME}"); //行首  System.Windows.Forms.SendKeys.SendWait("+{END}"); //選中當(dāng)前行  return true;  }  else  {  hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Edit", string.Empty);  if (hwnd.ToInt32() == 0)   return false;  else  {   SetForegroundWindow(hwnd);   System.Windows.Forms.SendKeys.SendWait("^{HOME}");//將光標(biāo)定位到首行   System.Windows.Forms.SendKeys.SendWait("{DOWN " + (iRow - 1) + "}");   System.Windows.Forms.SendKeys.SendWait("{HOME}"); //行首   System.Windows.Forms.SendKeys.SendWait("+{END}"); //選中當(dāng)前行  }  }  return true; } }}

調(diào)用:

string path = @"C:/Users/ZKK/Desktop/English.txt";bool res = PositionNotepad.PositionNotePad(path, "5");

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 林甸县| 明星| 永川市| 平阴县| 大名县| 秦安县| 襄城县| 黔西| 长沙市| 十堰市| 东兰县| 新蔡县| 涞水县| 永登县| 股票| 镇雄县| 琼结县| 武川县| 东乡县| 湛江市| 嘉荫县| 巢湖市| 都江堰市| 永泰县| 永德县| 新巴尔虎右旗| 大方县| 长春市| 永顺县| 桓仁| 清水河县| 重庆市| 日喀则市| 禄劝| 伊通| 肇庆市| 濮阳县| 孟州市| 宜城市| 霍邱县| 徐汇区|