如何在buttonclick上找到文件夹中的txt文件名 [英] How to find txt file name from a folder on buttonclick

查看:72
本文介绍了如何在buttonclick上找到文件夹中的txt文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件夹中有很多.txt文件,所有txt文件都包含一些信息。我有两个文本框第一个文本框用于该文件夹的路径,其中txt文件存在我只放文件夹路径而不是txt路径。在第二个文本框中键入一个像玫瑰的单词,然后单击提交按钮然后我如何找到哪个txt文件包含那个单词Rose以及我如何将txt文件名存储在变量中。





i这样写代码......



i have lots of .txt file in a folder and all txt file contains some information. i have two text box 1st text box use for path of that folder where txt file present i put only folder path not txt path.In second text box type a word like Rose and click on submit button then how i find in which txt file contains that word "Rose" and how i store that txt file name in a variable.


i write code like this...

string Path = Convert.ToString(txtBrowse.Text.Trim());
            string Word = Convert.ToString(txtWord.Text.Trim());

          
            foreach (string file in Directory.GetFiles(Path, "*.txt"))
            {
                contents = File.ReadAllText(file);

                if (contents.Contains(Word))
                {
                    string foldername = file;

                    lbltab.Text = foldername;

                    break;
                }
            }





它给了我完整的路径,但我只想要txt文件名。





请帮我解决一下......

谢谢...



it gives me full path but i want only txt file name.


please help me solve it...
thanks...

推荐答案

为文件夹中的所有文本文件制作循环,并使用阅读器读取每个文件,并将您的数据(例如:玫瑰)与读取数据进行比较。
Make loop for all text files in a folder and read each file using reader and compare your data(Ex:Rose) with read data.


您需要的是 Path.GetFileName方法 [ ^ ]



what you need is Path.GetFileName Method[^]

string fileName = @"C:\mydir\myfile.ext";
string result = Path.GetFileName(fileName);//returns 'myfile.ext' 


这篇关于如何在buttonclick上找到文件夹中的txt文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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