Vb.net 在第一、第二、第三等分割之前获取完整字符串
我有一个文件位置,我需要检查它是否存在 我不想这样做:Vb.net 在第一、第二、第三等分割之前获取完整字符串,vb.net,split,Vb.net,Split,我有一个文件位置,我需要检查它是否存在 我不想这样做: Dim route As String = ("C:\testing1\testing2\testing3\testing4\testing5\TEXTBESTAND.txt") If System.IO.File.Exists(route) Then MsgBox("BESTAAT HET WERKT!") Else
Dim route As String = ("C:\testing1\testing2\testing3\testing4\testing5\TEXTBESTAND.txt")
If System.IO.File.Exists(route) Then
MsgBox("BESTAAT HET WERKT!")
Else
Dim subroute() As String = route.Split("\"c)
Dim counting As Integer = route.Split("\"c).Length - 1
For count2 As Integer = 0 To counting - 1
Dim firstbackslash As Integer = route.IndexOf("\")
Dim backslash As Integer = route.IndexOf("\", firstbackslash + 1)
Dim firstPart As String = route.Substring(0, backslash)
MsgBox(firstPart)
Next
我试图做到的是,我首先检查文件夹“C:”是否存在,然后是“C:\testing1”,然后是“C:\testing1\testing2”等等
但是我在互联网上找不到类似的东西,也找不到一些乱七八糟的东西…不要使用字符串操作来处理文件或文件夹路径。使用
路径
类
一种选择:
私有函数GetExistingSubPath(完整路径为字符串)为字符串
如果Directory.Exists(fullPath)或lse File.Exists(fullPath),则
返回完整路径
如果结束
Dim子路径=Path.GetDirectoryName(完整路径)
如果子路径为Nothing,则
一无所获
如果结束
返回GetExistingSubPath(子路径)
端函数
示例用法:
Dim fullPath=“C:\testing1\testing2\testing3\testing4\testing5\TEXTBESTAND.txt”
Dim existingSubPath=GetExistingSubPath(完整路径)
Console.WriteLine(现有子路径)
这是一个算法,它将为您提供从根开始到最终路径(包括文件名)的所有路径。您可以使用此选项检查每个文件夹,并在不存在文件夹时创建它们:
Sub Main()
Dim route As String = ("C:\testing1\testing2\testing3\testing4\testing5\TEXTBESTAND.txt")
Dim fi As New System.IO.FileInfo(route)
If Not fi.Exists Then
Dim fileName As String = Path.GetFileName(route)
Dim di As DirectoryInfo = fi.Directory
Dim pathStack As New Stack(Of String)()
pathStack.Push(di.Name)
While Not IsNothing(di.Parent)
di = di.Parent
pathStack.Push(di.Name)
End While
Dim curPath As String = ""
While pathStack.Count > 0
curPath = Path.Combine(curPath, pathStack.Pop)
' ... do something with "curPath" in here ...
' ... like check for existence and create it ...
Console.WriteLine(curPath)
End While
curPath = Path.Combine(curPath, fileName)
' ... do something with "curPath" in here ...
' ... this is the full path including the file on the end ...
Console.WriteLine(curPath)
End If
Console.Write("Press Enter to quit...")
Console.ReadLine()
End Sub
输出:
C:\
C:\testing1
C:\testing1\testing2
C:\testing1\testing2\testing3
C:\testing1\testing2\testing3\testing4
C:\testing1\testing2\testing3\testing4\testing5
C:\testing1\testing2\testing3\testing4\testing5\TEXTBESTAND.txt
Press Enter to quit...
我试图做到的是,我首先检查文件夹“C:”是否存在,然后是“C:\testing1”,然后是“C:\testing1\testing2”等等
不
这根本不是怎么做的!文件系统是不稳定的:在每次检查之间,情况可能会发生变化。此外,文件存在只是阻止文件访问的众多因素之一
更好的做法是尝试访问有问题的文件,然后在异常失败时处理异常。请记住,由于上一段的原因,您必须能够处理这里的异常
.Exists()
不会使您免于编写该代码。每一次检查都是另一轮磁盘访问,这大概是在计算机中可能做的最慢的事情了。。。甚至比为异常展开堆栈还要慢,这是对此想法的常见反对意见。我修复了它,知道我可以检查多个文本文件是否在我需要的位置,如果没有,我将文本文件放在我需要的位置。然后我可以在里面加些东西。(我需要在其中放置其他内容的位置。)
看看
System.IO.Path
我知道这不是最好的方法。但我只需要检查是否有(这是99999%的时间),如果没有。我需要在那个位置放一个文本文件,里面有东西,然后把文件放在那个位置,如果失败了,处理异常。无论如何,你必须能够做到这一点。我还没有那么好,我只编写了几个月的视图代码。我是通过做一些事情并失败了很多而学到这一点的;)。如果你知道一些很好的VB.Net代码书,我想知道。
Dim een As String = "C:\testing1\testing2\testing7\testing1\testing1\text.txt"
Dim twee As String = "C:\testing1\testing2\testing7\testing2\testing1\text.txt"
Dim drie As String = "C:\testing1\testing2\testing7\testing3\testing1\text.txt"
Dim vier As String = "C:\testing1\testing2\testing7\testing4\testing1\text.txt"
Dim Files As String() = {een, twee, drie, vier}
For Each route As String In Files
If System.IO.File.Exists(route) Then
MsgBox("BESTAAT HET WERKT!")
Else
Dim subroute() As String = route.Split("\"c)
Dim counting As Integer = route.Split("\"c).Length - 1
Dim tel As Integer = route.Substring(route.LastIndexOf("\") + 1).Count
Dim bestandnaam As String = route.Substring(route.LastIndexOf("\") + 1)
For count2 As Integer = 0 To route.Length - tel - 1
Dim firstbackslash As Integer = route.IndexOf("\", count2)
Dim backslash As Integer = route.IndexOf("\", count2)
Dim Mapnaam As String = route.Substring(0, backslash)
count2 = firstbackslash
If System.IO.Directory.Exists(Mapnaam) Then
Else
System.IO.Directory.CreateDirectory(Mapnaam)
End If
Next
If System.IO.File.Exists(route) Then
Else
Dim objStreamWriter As System.IO.StreamWriter
objStreamWriter = New System.IO.StreamWriter(route)
Dim label As String
Select Case route
Case een
label = "een"
Case twee
label = "twee"
Case drie
label = "drie"
Case vier
label = "vier"
End Select
Dim value As String = InputBox("Route invullen naar " & label)
'Dim objStreamWriter As New System.IO.StreamWriter(route)
objStreamWriter.Write(value)
objStreamWriter.Close()
'Using sw As System.IO.StreamWriter = System.IO.File.AppendText(route)
' sw.WriteLine(value)
'End Using
End If
End If
Next route