我尝试在磁盘中查找文件,然后进行更改。我可以使用文件对话框来完成此操作,但问题是该磁盘未显示在计算机文件夹中(应该是这样的)。因此,我尝试查找该文件在哪个磁盘中,然后保存文件名,以便进行更改(是文本文件)。
看起来代码找到了文件,但是之后我无法保留文件名,也许在尝试一些后可能与文件名有关,我看到文件正在使用中(我不确定何时执行该操作)
感谢您对Panos的任何帮助
Public Class Form1
Dim FileFOund As String
Private Sub FindFile()
Dim drives As String() = Directory.GetLogicalDrives()
Dim tempfile As String = "temp.txt"
For Each drive As String In drives
Dim driveFound As String = drive
If File.Exists(driveFound & tempfile) Then
Dim lines() As String = File.ReadLines(driveFound & tempfile)
If lines(1) = "#temp" Then
LblFileExists.Text = "Temp found"
FileFOund = driveFound & tempfile
Else
LblFileExists.Text = "Temp not found"
End If
Else
LblFileExists.Text = "Temp found"
End If
End If
Next
End Sub
End Class
然后我需要在fileFound内更改一行
Private Sub Button1_MouseClick(sender As Object, e As MouseEventArgs) Handles Button1.Click
If System.IO.File.Exists(fileFound) Then
Dim lines() As String = IO.File.ReadAllLines(fileFound)
For i As Integer = 0 To lines.Length - 1
If lines(i).Contains("Anything") Then
lines(i) = "to change"
End If
Next
File.WriteAllLines(fileFound, lines)
MessageBox.Show("Changes complete!", "Temp")
Else
MessageBox.Show("Something Wrong!", "Temp")
End If
End Sub
希望您一切都清楚,再次感谢。
答案 0 :(得分:0)
Public Class Form1
Dim FileFOund As String
Private Sub FindFile()
Dim drives As String() = Directory.GetLogicalDrives()
For Each drive As String In drives
Dim driveFound As String = drive
If File.Exists(driveFound & tempfile) Then
FileFOund = driveFound & tempfile
End If
Next
If File.Exists(driveFound & "temp.txt") Then
Dim lines() As String = Filter(File.ReadAllLines(driveFound & "temp.txt"), "#Temp")
If UBound(lines) = 0 Then
LblFileExists.Text = "Temp found"
Else
LblFileExists.Text = "Temp not found"
End If
Else
LblFileExists.Text = "Temp found"
End If
End Sub
Private Sub Button1_MouseClick(sender As Object, e As MouseEventArgs) Handles Button1.Click
If System.IO.File.Exists(fileFound) Then
Dim lines() As String = IO.File.ReadAllLines(fileFound & "temp.txt")
For i As Integer = 0 To lines.Length - 1
If lines(i).Contains("Anything") Then
lines(i) = "to change"
End If
Next
File.WriteAllLines(fileFound & "temp.txt", lines)
MessageBox.Show("Changes complete!", "Temp")
Else
MessageBox.Show("Something Wrong!", "Temp")
End If
End Sub
End Class