C#搜索文字在文件及文件夾中出現(xiàn)位置的方法
本文實(shí)例講述了C#搜索文字在文件及文件夾中出現(xiàn)位置的方法。分享給大家供大家參考。具體如下:
在linux中查詢(xún)文字在文件中出現(xiàn)的位置,或者在一個(gè)文件夾中出現(xiàn)的位置,用命令:
就可以了。今天做了一個(gè)C#程序,專(zhuān)門(mén)用來(lái)找出一個(gè)指定字符串在文件中的位置,與一個(gè)指定字符串在一個(gè)文件夾中所有的出現(xiàn)位置。
一、程序代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Search
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 3 || (args[0] != "file" && args[0] != "folder"))
{
Console.WriteLine("Correct Order Style: ");
Console.WriteLine("Search file/folder address word");
}
switch (args[0])
{
case "file": //從文件中查找
{
if (System.IO.File.Exists(args[1]))
{
FindInFile(args[1], args[2]);
}
else
{
Console.WriteLine(string.Format(
"File {0} not exist!", args[1]));
}
}
break;
case "folder": //從文件夾中查找(包括其中全部文件)
{
if (System.IO.Directory.Exists(args[1]))
{
FindInDirectory(args[1], args[2]);
}
else
{
Console.WriteLine(string.Format(
"Directory {0} not exist!", args[1]));
}
}
break;
default: break;
}
Console.WriteLine("Output Finished.");
Console.ReadLine();
}
/// <summary>
/// 從文件中找關(guān)鍵字
/// </summary>
/// <param name="filename"></param>
/// <param name="word"></param>
public static void FindInFile(string filename, string word)
{
System.IO.StreamReader sr = System.IO.File.OpenText(filename);
string s = sr.ReadToEnd();
sr.Close();
string[] temp = s.Split('\n');
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].IndexOf(word) != -1)
{
Console.WriteLine(string.Format(
"Found in: {0}\n{1}\nLine: {2} \n",
filename, temp[i].Trim(), i + 1));
}
}
}
/// <summary>
/// 從文件夾中找關(guān)鍵字
/// </summary>
/// <param name="foldername"></param>
/// <param name="word"></param>
public static void FindInDirectory(string foldername, string word)
{
System.IO.DirectoryInfo dif = new System.IO.DirectoryInfo(foldername);
//遍歷文件夾中的各子文件夾
foreach (System.IO.DirectoryInfo di in dif.GetDirectories())
{
FindInDirectory(di.FullName, word);
}
//查詢(xún)文件夾中的各個(gè)文件
foreach (System.IO.FileInfo f in dif.GetFiles())
{
FindInFile(f.FullName, word);
}
}
}
}
二、運(yùn)行示例
查找文件 E:\TestProgram\Search\Search\Program.cs 中所有的 Console
在程序Search.exe所在目錄下,輸入命令:Search file/folder 地址 要查找的字符串

三、關(guān)于VS測(cè)試帶有輸入?yún)?shù)的程序
在項(xiàng)目屬性→調(diào)試選項(xiàng)卡→啟動(dòng)選項(xiàng)→命令行參數(shù),把參數(shù)輸入進(jìn)去就可以了

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)EPL?II格式打印與打印測(cè)試
這篇文章介紹了C#實(shí)現(xiàn)EPL?II格式打印與打印測(cè)試的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#實(shí)現(xiàn)的Excel文件操作類(lèi)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的Excel文件操作類(lèi),結(jié)合具體實(shí)例形式分析了C#數(shù)據(jù)庫(kù)及Excel文件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Unity基于ShaderLab實(shí)現(xiàn)光照系統(tǒng)(著色器代碼實(shí)現(xiàn)小結(jié))
這篇文章主要介紹了Unity基于ShaderLab實(shí)現(xiàn)光照系統(tǒng),主要總結(jié)unity中shaderlab的著色器代碼實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#郵件定時(shí)群發(fā)工具Atilia用法實(shí)例
這篇文章主要介紹了C#郵件定時(shí)群發(fā)工具Atilia用法,較為詳細(xì)的分析了Atilia實(shí)現(xiàn)郵件定時(shí)群發(fā)功能的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#實(shí)現(xiàn)自定義光標(biāo)并動(dòng)態(tài)切換
這篇文章主要為大家詳細(xì)介紹了如何利用C#語(yǔ)言實(shí)現(xiàn)自定義光標(biāo)、并動(dòng)態(tài)切換光標(biāo)類(lèi)型,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07
c#實(shí)現(xiàn)萬(wàn)年歷示例分享 萬(wàn)年歷農(nóng)歷查詢(xún)
這篇文章主要介紹了c#實(shí)現(xiàn)萬(wàn)年歷的方法,可以顯示農(nóng)歷、節(jié)氣、節(jié)日、星座、星宿、屬相、生肖、閏年月、時(shí)辰,大家參考使用吧2014-01-01

