C# 搜索文字在文件、文件夹中的出现位置

本文介绍了一个简单的C#程序,该程序能够帮助用户查找指定字符串在一个文件或整个文件夹(包括子文件夹及其文件)中的具体位置。通过命令行参数,可以灵活指定搜索目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

〇、关于本文

在linux中查询文字在文件中出现的位置,或者在一个文件夹中出现的位置,用命令

grep -n '需要查询的文字' *

就可以了。今天做了一个C#程序,专门用来找出一个指定字符串在文件中的位置,与一个指定字符串在一个文件夹中所有的出现位置。

一、程序代码

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>
        /// 从文件中找关键字
        /// </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>
        /// 从文件夹中找关键字
        /// </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);
            }

            //查询文件夹中的各个文件
            foreach (System.IO.FileInfo f in dif.GetFiles())
            {
                FindInFile(f.FullName, word);
            }
        }
    }
}

二、运行示例

查找文件 E:\TestProgram\Search\Search\Program.cs 中所有的 Console

在程序Search.exe所在目录下,输入命令:Search file/folder 地址 要查找的字符串

110504_hLTy_1425762.png

三、关于VS测试带有输入参数的程序

在项目属性→调试选项卡→启动选项→命令行参数,把参数输入进去就可以了

110718_wxvA_1425762.png

转载于:https://my.oschina.net/Tsybius2014/blog/287223

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值