⑴ 用.Net打开一个txt文件,怎么对文件里的内容进行查找,替换
简单的办法是
1、File.ReadAllText()用这个放大读出来文件内容,到一个字符串变内量
2、用Replace()这个方法替换字符串
3、再用容File.WriteAllText这个方法写回到文件
string text = File.ReadAllText("D:\\1.txt");
string result = text.Replace("oldStr", "NewStr");
File.WriteAllText("D:\\1.txt", result);
⑵ 文件夹有多个txt,vb.net 怎么逐一打开txt文档,并对其中某两行进行检索,找满足条件的文档
1,逐个搜索文件夹里的所有TXT文件。
2,每搜索到一个TXT文件就进行对应操作。
⑶ vb.net如何打开选定文件夹下所有TXT文件,读取数据,写入数据,并保存数据至新的文件
IfFolderBrowserDialog.ShowDialog=Windows.Forms.DialogResult.OKThen
Dim资料夹AsString()=System.IO.Directory.GetFiles(FolderBrowserDialog.SelectedPath,"*")
ForEach文件In资料夹
MsgBox(My.Computer.FileSystem.ReadAllText(文件))'读取数据
My.Computer.FileSystem.WriteAllText(文件,"数据",False)'写入数据
Next
EndIf
EndUsing
⑷ vb.net 如何打开txt文件
说明:以下代码在Microsoft Visual Basic 2005 (简体中文版)中通过。
创建新项目:
在窗体上添加文本框2个:TextBox1,TextBox2
TextBox1 -- 用来编辑要写入的文本文件的内容,或显示打开的文本文件的内容
TextBox2 -- 用来输入要打开或要写入的文件名(包括盘符,路径)(例如:c:\123.txt)
在窗体上添加2个按钮:Button1,Button2
Button1 -- 写入文件
Button2 -- 打开文件
代码如下:
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim w As New StreamWriter(TextBox2.Text)
        w.Write(TextBox1.Text)
        w.Close()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim r As New StreamReader(TextBox2.Text)
        Dim s As String
        TextBox1.Text = ""
        Do While r.Peek > -1 '是否到文件尾
            s = r.ReadLine
            '            MessageBox.Show(r.Peek)
            TextBox1.Text = TextBox1.Text & s & vbCrLf
        Loop
        r.Close()
    End Sub
End Class 
 
补充:你要把读出的数据赋值给一个变量,只要:声明一个变量为数值类型,然后只要读取一行就可以了,把这行数据经过转换成数值后赋给这个变量.
⑸ vb.net文件读取txt
读取每行TXT如下代码:
VB.NETcode
PublicSubReadFileSample()
'打开程序当前路径下的config.txt文件
'内容就是楼主贴出来的
DimreaderAsTextReader=File.OpenText("config.txt")
DimlineAsString=reader.ReadLine()'读第一行
line=reader.ReadLine()'读第二行
line=reader.ReadLine()'读第三行
DimnAsInteger=3'当前行号
Whileline<>""Andn<50
line=reader.ReadLine()'读下一行
n=n+1
EndWhile
DimitemsAsString()=line.Split("".ToCharArray(),StringSplitOptions.RemoveEmptyEntries)
ForEachitemAsStringInitems
Console.WriteLine(item)
Next
EndSub
⑹ vb.net中怎样在菜单栏点打开文件来打开D盘中一个txt文件
import System.IO
用文件流读
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim open As New OpenFileDialog
        open.Filter = "テキスト ファイル (*.txt)|*.txt" & _
                      "|すべてのファイル (*.*)|*.*"
        If open.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = open.FileName
            Dim file As New FileStream(open.FileName, FileMode.Open, FileAccess.Read)
            Dim encoding As Encoding = encoding.GetEncoding(Util.ToString(Me.ComboBox1.SelectedItem))
            Dim stream As New StreamReader(file, encoding)
            Dim strB As New StringBuilder
            While (Not stream.EndOfStream)
                strB.AppendLine(stream.ReadLine)
            End While
            Me.RichTextBox1.Text = strB.ToString
            stream.Close()
        End If
    End Sub
⑺ vb .net 程序怎么默认打开txt记事本文件
楼下的根本不对。exe右键是没有“打开方式”的,也没必要因为这个问题而还原系统。把exe设置为直接运行,方法:新建一个txt文件,输入ftype exefile="%1" %* ,保存,把扩展名改为bat,双击这个文件,就好了。
⑻ 在.NET中怎么读取.txt文件
class Program
    {
        static void Main(string[] args)
        {
            //使用StreamReader来读取一个文本文件
            //using (StreamReader sr = new StreamReader(@"C:\Users\SpringRain\Desktop\抽象类特点.txt",Encoding.Default))
            //{
            //    while (!sr.EndOfStream)
            //    {
            //        Console.WriteLine(sr.ReadLine());
            //    }
            //}
//使用StreamWriter来写入一个文本文件
            using (StreamWriter sw = new StreamWriter(@"C:\Users\SpringRain\Desktop\newnew.txt",true))
            {
                sw.Write("看我有木有把你覆盖掉");
            }
            Console.WriteLine("OK");
            Console.ReadKey();
        }
    }
⑼ asp.net如何在点击按钮时打开一个txt文档
是要读取里面的内容吧!
Response.Write(GetInterIDList(fileName));//fileName是文件的物理路径
//读取txt文件的内容
    public string GetInterIDList(string strfile)
    {
        string strout;
        strout = "";
        if (!File.Exists(strfile))
        {
            SDLX.Common.MessageBox.ShowMobile(this, "您要打开的文件不存在!");
        }
        else
        {
            StreamReader sr = new StreamReader(strfile, System.Text.Encoding.Default);
            String input = sr.ReadToEnd();
            sr.Close();
            strout = input;
        }
        return strout;
    }