使用拆分读取文本文件,保存信息并在子VB中使用 [英] Read text file using split, save the info and use in sub VB
问题描述
有一个文本文件,其中包含我需要的几个不同的C:\位置,我们的想法是使用streamreader读取文本文件,将每一行加载到一个数组中,然后在每行中使用该特定数组子程序代码。
的想法,就是如果将来C:\的位置发生变化,就没有必要进入代码了,只修改那个txt文件的信息就可以了......
我试过的:
尝试了几种但没有运气...
文本文件信息看起来像
; C:\ceceiveddata\d1.html
; C:\ceceiveddata\d2.html
; C:\ displaydata\d1.html
; C:\ displaydata \ d2.html
我也试过把;什么都没有,并把文本放在没有什么
的想法是修改setup.txt文件,并更新要用于的新位置程序......
这里是代码....我试过以下但没有运气
Public Class Form1
Dim urls(10)As String
Public Sub Setup()
Dim sr As New StreamReader(C:\ setup.\\Setup.txt)
Dim line As String
Dim token()作为String
Dim i As Integer = 0
Do to sr.Peek = -1
'grab从txt一次一行
line = sr.ReadLine()
'放入数组
token = Split(line,line, ;)
urls(i)= token(0)
'增量循环为i
i = i + 1
Loop
sr.Close()
End Sub
Private Sub Form1_Load(发件人作为对象,e作为EventArgs)处理MyBase.Load
呼叫设置()'每个网址的加载设置在项目中
调用displayfolderlog()'检查文件夹是否存在
调用MoveFiles()
结束子
< br $>
Public Sub MoveFiles()
Dim D2Source =C:\ceceiveddata\d2.html;
Dim D2file =C:\displaydata\d2.html;
'FOR D1 FILE
如果My.Computer.FileSystem.FileExists(urls(1))= True那么
My.Computer.FileSystem.MoveFile(urls(1),urls(3))
结束如果
'FOR D2 FILE
如果My.Computer.FileSystem.FileExists(D2Source)= True那么
My.Computer.FileSystem.MoveFile(D2Source,D2file)
结束如果
End Sub
结束班
它只移动D2文件,但D1没有....
从我对你的问题的理解,我认为你想从文本文件中读取任意数量的行,每行是一个文件路径。
我认为你通过逐行阅读文件然后尝试进一步拆分它来让自己有点困惑。
输入文件
C:\receiveddata\d1.html< CR>< LF>
C:\ceceiveddata\d2.html< CR>< LF>
C:\displaydata \d1.html< CR>< LF>
C:\displaydata \d2.html< CR>< LF>
您不需要特定的分隔符,因为您已经拥有虽然在常规文本编辑器中不可见,但每行末尾有回车符和换行符。
下一步是读取行。
System.IO.File.ReadAllLines [ ^ ]是为这项任务而做的。
基本上所有你需要做的就是:
Dim filePath As String
对于 每个 filePath 在 File.ReadAllLines( C:\ Setup \ Setup.txt)
' 对文件做一些很酷的事情。
下一步跨度>
have a Text file with several different C:\ locations i need, the idea is to use the streamreader to read the text file, load each line into an array and then be able to use that specific array per line in the Sub routines to code.
the idea, is that if in the future the location of the C:\ changes, there is no need to go into the code, just modify the information on that txt file and that would be it...
What I have tried:
have tried several ways but no luck...
text file info looks like
;C:\receiveddata\d1.html
;C:\receiveddata\d2.html
;C:\displaydata\d1.html
;C:\displaydata\d2.html
I also tried putting the ; at the en andd nothing and putting the text in "" and nothing
the idea is to modify the setup.txt file and that updates the new location to be used in the program...
here is the code .... i tried the following and no luck
Public Class Form1
Dim urls(10) As String
Public Sub Setup()
Dim sr As New StreamReader("C:\Setup\Setup.txt")
Dim line As String
Dim token() As String
Dim i As Integer = 0
Do Until sr.Peek = -1
'grab one line at a time from the txt
line = sr.ReadLine()
'place into an array
token = Split(line, ";")
urls(i) = token(0)
'increment loop for i
i = i + 1
Loop
sr.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Setup() 'load setup for each url in project
Call displayfolderlog() 'check if folders exist
Call MoveFiles()
End Sub
Public Sub MoveFiles()
Dim D2Source = "C:\receiveddata\d2.html";
Dim D2file = "C:\displaydata\d2.html";
'FOR D1 FILE
If My.Computer.FileSystem.FileExists(urls(1)) = True Then
My.Computer.FileSystem.MoveFile(urls(1), urls(3))
End If
'FOR D2 FILE
If My.Computer.FileSystem.FileExists(D2Source) = True Then
My.Computer.FileSystem.MoveFile(D2Source, D2file)
End If
End Sub
End Class
it only moves the D2 file but the D1 no....
From my understanding of your problem, I gather that you want to read an arbitrary number of lines from a text file and each line is a file path.
I think you are confusing your self a bit by first reading the file line-by-line and then try to split it further.
Input File
C:\receiveddata\d1.html<CR><LF> C:\receiveddata\d2.html<CR><LF> C:\displaydata\d1.html<CR><LF> C:\displaydata\d2.html<CR><LF>
You don't need a specific separator character, because you already have a Carriage Return and a Line Feed at the end of each line, although invisible in the regular text editor.
The next step is to read the lines.
System.IO.File.ReadAllLines[^] was kind of made for this task.
So basically all you have to do is this:
Dim filePath As String For Each filePath In File.ReadAllLines("C:\Setup\Setup.txt") ' Do some cool stuff with your files. Next
这篇关于使用拆分读取文本文件,保存信息并在子VB中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!