我需要创建一个读取文本文件的程序,然后将这些文件分开。我想我的分割功能不起作用。
意图是文件中的所有文本都需要拆分并显示在ListBox1
中。如果我的代码中有一些奇怪的东西,那是因为我尝试了很多,但没有任何效果。
代码:
Imports System.IO
Imports System
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objFile As New System.IO.StreamReader("C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\Opdrachtje\Opdrachtje\bin\Debug\Images.txt")
Dim strTextFile As String
strTextFile = objFile.ReadLine
Dim somestring As String() = Split(",")
Dim somestring2() As String
somestring2 = TextBox1.Text.Split(",")
Dim text As String
For i = 0 To UBound(somestring2) - 1
text = strTextFile & " - "(somestring2(i))
Next i
Do Until strTextFile Is Nothing
ListBox1.Items.Add(text)
strTextFile = objFile.ReadLine()
Loop
objFile.Close()
objFile.Dispose()
End Sub
End Class
以下是Images.txt
文件中的几行。
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000001.tif,1,268432
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000001.tif,1,17560
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000002.tif,1,264276
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000002.tif,1,15706
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000003.tif,1,263212
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000003.tif,1,16836
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000004.tif,1,261722
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000004.tif,1,16786
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000005.tif,1,268062
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000005.tif,1,18514
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000006.tif,1,250534
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000006.tif,1,15536
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000007.tif,1,249188
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000007.tif,1,15280
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000008.tif,1,255074
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000008.tif,1,16324
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000009.tif,1,277774
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000009.tif,1,18174
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000010.tif,1,260760
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000010.tif,1,17212
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000011.tif,1,257688
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000011.tif,1,16332
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000012.tif,1,265722
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000012.tif,1,16610
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000013.tif,1,265530
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000013.tif,1,18016
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000014.tif,1,259384
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000014.tif,1,16698
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000015.tif,1,255404
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000015.tif,1,16014
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000016.tif,1,276840
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000016.tif,1,17198
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000017.tif,1,279526
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000017.tif,1,16774
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000018.tif,1,262920
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000018.tif,1,17132
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000019.tif,1,266274
N:\Opdrachten\2363-3255-01\0 - Origineel Binair\2017-03-08\00000019.tif,1,16418
答案 0 :(得分:2)
首先,转动Option Strict On:
将隐式数据类型转换限制为仅扩展转换,禁止后期绑定,并禁止导致Object类型的隐式类型。
您的某些代码会在 Option Strict On 时产生编译错误。 始终启用此设置是一种很好的做法。
其次,考虑实施Using:
托管资源由.NET Framework垃圾收集器(GC)处理,无需您进行任何额外编码。对于托管资源,您不需要使用“使用”块。但是,您仍然可以使用“使用”块来强制处理受管资源,而不是等待垃圾回收器。
现在解决手头的问题。我相信你想用逗号(,
)分割文本行。举个例子:
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000001.tif,1,268432
会变成:
N:\Opdrachten\2363-3255-01\0 - Origineel Kleur\2017-03-08\00000001.tif
1
268432
然后,您希望在ListBox
。
如果我在这个假设中是正确的,那么我首先建议使用ListView,因为你可以更整洁地显示信息。在ListView
添加3列,并将View
属性设置为详细信息。
以下代码将拆分每一行并将其添加到ListView
:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using sr As New System.IO.StreamReader(filename)
While Not sr.EndOfStream
Dim lines As String() = sr.ReadLine.Split(New String() {","}, StringSplitOptions.None)
Dim lvItem As New ListViewItem(lines(0))
lvItem.SubItems.Add(lines(1))
lvItem.SubItems.Add(lines(2))
ListView1.Items.Add(lvItem)
End While
End Using
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
这是输出的屏幕截图:
显然,您希望为每个ColumnHeader
提供一个更好的名称,但这应该可以让您继续。
由于我不是100%在您想要的输出上,我还会为您的ListBox
提供解决方案,并且只显示第一部分。它与ListView
解决方案类似:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using sr As New System.IO.StreamReader("C:\Images.txt")
While Not sr.EndOfStream
Dim lines As String() = sr.ReadLine.Split(New String() {","}, StringSplitOptions.None)
ListBox1.Items.Add(lines(0))
End While
End Using
End Sub
这是输出的屏幕截图:
答案 1 :(得分:1)
您可以使用Microsoft.VisualBasic'中的Microsoft.VisualBasic.FileIO.TextFieldParser类。程序集将文件解析为CSV文件,在c#中:
var header = new List<string>();
var data = new List<string[]>();
var line = 0;
using (var parser = new TextFieldParser(filePathOrStream, System.Text.Encoding.UTF8))
{
parser.CommentTokens = new string[] { "#" };
parser.SetDelimiters(new string[] { "," });
parser.HasFieldsEnclosedInQuotes = true;
try
{
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
if (line == 0)
{
header.AddRange(fields);
}
else
{
data.Add(fields);
}
line++;
}
}
catch (MalformedLineException mfex)
{
// error handler
}
}
答案 2 :(得分:0)
我建议的第一件事就是始终将文件路径放入自己的变量中,这样可以使代码更易于阅读和更改。
正如评论中所述,还要考虑以这种方式读取文件,因为不确定您是否正确使用StreamReader。 System.IO.File.ReadAllLines Method这会更容易。
我已在代码中留下评论以帮助
这应该有助于您入门:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
dim filepath as String = "C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\Opdrachtje\Opdrachtje\bin\Debug\Images.txt"
Dim objFile as List(of String) = New List(Of String)
objFile.AddRange(File.ReadAllLines(filepath))
'now have a list with all your file written into it. It is now possible to loop through it later
Dim somestring2 As List(Of String) = New List(Of String) 'something2 needs to be more of a descriptive name as not sure what it's meant for at this stage in reading the code. I also changed this to a list as list are better and easier to use than arrays, though you will have to google why.
'loop through file and read into array.
For Each record as String in objFile
somestring2.Add(record.Split(","c))
Next
'you now have a list with everything split down using comma as a delimator.
'Now here you can add to the listbox. I'll leave this for you to do as the file has been read in correctly now.
End Sub