从文本文件中搜索项目(重新发布) [英] Search items from text file (re-post)

查看:51
本文介绍了从文本文件中搜索项目(重新发布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在C上有一个文本文件://file.txt

在该文本文件中包含 -

CodeProject

Coogle

代码

Cripsy

Cyclone

犯罪

犯罪



Din

完成



税务

服务台
Eagle

耳朵



.......



现在我的问题是 -

我有一个csharp winform应用程序

Button1

Textbox1

Listbox1

我想在button1上放一个代码 -

如果我在textbox1中写= E

在列表框上显示所有Word都以E开头

file.txt

如果我输入Do

然后显示

完成







我找到了这个答案 -



protected void Button_Click(object

sender,EventArgs e)

{

string fileLoc =

Server.MapPath(你Locati on);

if(File.Exists(fileLoc))

{

String line;

using (TextReader tr =

new StreamReader(fileLoc))

{

line = tr.ReadToEnd

() ;

}

string []

read = line.StartsWith

(textboxName.text);

for(int i = 0; i< read.length; i +>

+)

{

//你的逻辑;

}

}

}


来自Salman622的




但我很累。谁能告诉我哪里出错了?代码确实有误。



感谢高级

Suppose I have a textfile on C://file.txt
On that text file contains -
CodeProject
Coogle
Code
Cripsy
Cyclone
Crime
Criminal
Dog
Din
Done
Doing
Duty
Desk
Eagle
Ear
Eat
.......
etc
Now my question is -
I have a csharp winform application
Button1
Textbox1
Listbox1
I wanna put a code on button1-
If I write in textbox1= E
On listbox show all Word started with E from
file.txt
If I type "Do"
then show
Done
Doing
Dog

I found this answer -

protected void Button_Click( object
sender, EventArgs e)
{
string fileLoc =
Server.MapPath( "You Location" );
if (File.Exists(fileLoc))
{
String line;
using (TextReader tr =
new StreamReader(fileLoc))
{
line = tr.ReadToEnd
();
}
string[]
read=line.StartsWith
(textboxName.text);
for( int i=0;i<read.length;i+>
+)
{
//your logic;
}
}
}

from Salman622.

But I am tired. Could anyone show me where is wrong? code does error.

Thanks in advanced

推荐答案

请分析以下代码示例



Please analyze the following code sample

string searchWord = "abc"; //search work actually it comes from user input
            string filePath = @"D:\\Data\abc.txt"; //file location
            IEnumerable<string> dataList = File.ReadLines(filePath); //read all fine contain and store it to a list.
            IEnumerable<string> result = dataList.Where(d => d.StartsWith(searchWord));</string></string>





这里searchWord来自用户输入。使用linq where函数和StartWith函数。根据searchWord,结果将从您的文件中填充。它将返回适当的数据。如果只是将上面的代码放到您的应用程序事件处理程序中。



here searchWord comes from user input. Use linq where function and with StartWith function. Based on the searchWord the result will be populated from your file. It will return appropriate data. If just put the above code to your application event handler.


我提供的解决方案1是一个想法,您需要使用它几乎没有修改。您声称这不起作用但不提供您找到的详细错误信息。但是,我将提供完整的代码,我测试并发现使用.NET框架4.5与Windows窗体应用程序。

我只是在我的解决方案中添加一个示例WOrdFile.txt文件,每行都有换行符。

Solution 1 which i provide was an idea you need to use it with little modication. You claim that that is not working but not provide detail error information which you found. However i will provide full code which i tested and found working with .NET framework 4.5 with Windows forms application.
I just add a sample WOrdFile.txt file in my solution with line break of every word.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private IEnumerable<string> _wordList; 

        public Form1()
        {
            InitializeComponent();
            PopulateWordList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string searchWord = textBox1.Text;
            //search and populate the result
            IEnumerable<string> result = _wordList.Where(d => d.StartsWith(searchWord));

            //display the result in a listbox
            foreach (string word in result)
            {
                listBox1.Items.Add(word);
            }
        }

        //create word list from a source text file there word is stored with line break
        private void PopulateWordList()
        {
            string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) , "WOrdFile.txt");
            //do some defencive programming
            if (File.Exists(filePath))
            {
                _wordList = File.ReadLines(filePath);
            }
        }
    }
}





我创建了一个Windows窗体应用程序一个新的窗体,我放了一个文本框,一个列表框和一个按钮。代码是自我解释的。让我知道你在代码中不理解的任何事情。



I created a Windows Form app and a new windows form there i put one textbox, one listbox and one button. The code is self explanatory. Let me know anything you do not understand in code.


这篇关于从文本文件中搜索项目(重新发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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