我想从VB.NET中的文本文件中读取数据请帮帮我.. [英] I would like to read the data from the text file in VB.NET please help me..

查看:118
本文介绍了我想从VB.NET中的文本文件中读取数据请帮帮我..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从VB.NET中的文本文件中读取数据。但是,文本文件不在application.it中的任何文件夹中。必须只显示exe文件。文本文件直接添加到应用程序中。即,项目(右键单击) - >添加 - >现有项目 - >选择文本文件。



我尝试过:



i am尝试了流阅读器选项。但是,是不行的。因为文本文件不在任何文件夹中。它只在打开项目文件时显示(打开vb文件)。

I would like to read the data from the text file in VB.NET. but, the text file is not in any folders in the application.it must show only the exe file. the text file is added directly in the application. ie, project (right click) -> add -> existing item -> select the text file .

What I have tried:

i am tried the stream reader option. but, is does'nt work. because the text file not in any folders. it only show when open the project file(Open the vb file).

推荐答案

你需要使用反射来获取文本



1)在表格中添加这些和适当的引用



you need to use reflection to get the text

1) add these and appropriate references to your form

Imports System.IO
Imports System.Reflection





2)在适当的位置 - 一般声明区域,声明这些





2) In an appropriate spot - general declaration area, declare these

Dim _textStreamReader As StreamReader
Dim _assembly As [Assembly]





3)在表单加载事件中,添加下面的代码Private Sub Form1_Load和End Sub之间





3) in the form load event, add the code below between the Private Sub Form1_Load and the End Sub

// In Load Event For Form 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
     _assembly = [Assembly].GetExecutingAssembly()

     _textStreamReader = New StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"))
    Catch ex As Exception
        MessageBox.Show("Resource wasn't found!", "Error")
    End Try
End Sub





请注意:MyNamespace.MyTextFile.txt需要更改相应的命名空间和文本文件名你嵌入在你的项目中



4)假设你有一个button1来显示文本框TextBox1中文件的第一行,把这段代码放在事件处理程序中按钮点击





note this : MyNamespace.MyTextFile.txt needs to be changes to the appropriate namespace and text file name you embedded in your project

4) assuming you have a button1 to display the 1st line of the file in a textbox TextBox1, put this code in the event handler for the button click

// Button1 Click Handler 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
     If _textStreamReader.Peek() <> -1 Then
           TextBox1.Text = _textStreamReader.ReadLine()
        End If
     Catch ex As Exception
        MessageBox.Show("Error reading stream!")
    End Try
End Sub





5)如果你已经做到这一点,用_textStreamReader做你需要的任何事情处理您的文本文件



5) if youve made it this far, do whatever you need with _textStreamReader to process your text file


这篇关于我想从VB.NET中的文本文件中读取数据请帮帮我..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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