记事本中有一组文件名。如何在记事本中读取文件名并在特定文件夹中搜索文件并使用C#将该特定文件移动到另一个文件夹? [英] There are set of file name in the notepad.How to read the file name in notepad and search the file in a particular folder and move that particular file to another folder using C#?

查看:76
本文介绍了记事本中有一组文件名。如何在记事本中读取文件名并在特定文件夹中搜索文件并使用C#将该特定文件移动到另一个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取记事本中的内容并在一个文件夹中搜索内容文件并将文件移动到其他文件夹? Plz帮我在windows窗体应用程序中开发

How to read a content in notepad and search the content file in one folder and move the file to other folder ? Plz help me to develope in windows form application

推荐答案

请参阅样本供您参考。



Please see the sample for your reference.

//search Content
            string sSearchContent = "Search Content";
            //Directory Path where all notepad file exist
            string sSourceFilesDirectory = "Directory Path";
            //Path where file moved if content matched in the file
            string sMoveFileDirectoryPath = "";
            List<string> lstContentMatchedFile = new List<string>();
            //Get all notepad file from directory
            string [] arrFiles = Directory.GetFiles(sSourceFilesDirectory, "*.txt");
            string sFileContent="";
            foreach (string sFile in arrFiles)
            {
                sFileContent = File.ReadAllText(sFile);
                if (sFileContent.Contains(sSearchContent))
                {
                    lstContentMatchedFile.Add(sFile);
                }
            }

            //Code to move file
            foreach (string sFile in lstContentMatchedFile)
            {
                File.Move(sFile, Path.Combine(sMoveFileDirectoryPath, Path.GetFileName(sFile)));
            }


1。阅读文本文件中的文件名,使用 File.ReadAllLines [ ^ ]。

2.迭代通过文件名,并使用 File.Move [ ^ ]移动文件。
1. Read the file names in the text files, use File.ReadAllLines[^].
2. Iterate through the file names, and use File.Move[^] to move the file.


这篇关于记事本中有一组文件名。如何在记事本中读取文件名并在特定文件夹中搜索文件并使用C#将该特定文件移动到另一个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆