如何获取文件路径 [英] How to Get File Path

查看:69
本文介绍了如何获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个快速的功能,它只会给我一个文件的路径。

例如,如果我有字符串C:\Temp \ MyFile.txt ;,我想只返回

字符串C:\Temp \。如何使用VB.NET完成?


谢谢

Dustin

解决方案

< blockquote>这样你的意思是:


Dim file As String =" C:\Temp \ MyFile.txt"

Dim path As String = file.Substring(0,file.LastIndexOf(" \"))


我没试过但是应该可以。

Dustin Wilson写道:

我正在寻找一个快速的功能,它只能给我一个文件的路径。
例如,如果我有字符串 C:\Temp \ MyFile.txt",我只想返回
字符串C:\Temp \。如何使用VB.NET完成?

谢谢
Dustin



Dim x As String =" c:\ MyDy \ MyFile.txt"

Dim y As String = x.Substring(0,x.LastIndexOf(" \")+ 1)


我可能会复制一些内部VB函数......但是如果我是,那我还没有发现它... b / b
***通过开发人员指南 http://www.developersdex.com 发送***


我认为你应该使用目录信息对象,这里有一个例子:

__________________


进口系统

进口System.IO


公共类MoveToTest


Public Shared Sub Main()

''引用一个目录。

Dim di As New DirectoryInfo(" TempDir")


''创建目录只有它还没有ist。

如果di.Exists = False那么

di.Create()

结束如果


''在刚创建的目录中创建一个子目录。

Dim dis As DirectoryInfo = di.CreateSubdirectory(" SubDir")

Console.WriteLine(& '{0}'的根路径是''{1}''",dis.Name,

dis.Root)


' '删除父目录。

di.Delete(True)

End Sub''Main

End Class''MoveToTest

I''m looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin

解决方案

Somthing like this you mean:

Dim file As String = "C:\Temp\MyFile.txt"
Dim path As String = file.Substring(0, file.LastIndexOf("\"))

I didn''t try it but that should work.

Dustin Wilson wrote:

I''m looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin



Dim x As String = "c:\MyDir\MyFile.txt"
Dim y As String = x.Substring(0, x.LastIndexOf("\") + 1)

I might be duplicating some internal VB function...but if I am, I
haven''t discovered it yet...
*** Sent via Developersdex http://www.developersdex.com ***


I think you should use the directory info object, here an example:
__________________

Imports System
Imports System.IO

Public Class MoveToTest

Public Shared Sub Main()
'' Make a reference to a directory.
Dim di As New DirectoryInfo("TempDir")

'' Create the directory only if it does not already exist.
If di.Exists = False Then
di.Create()
End If

'' Create a subdirectory in the directory just created.
Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
Console.WriteLine("The root path of ''{0}'' is ''{1}''", dis.Name,
dis.Root)

'' Delete the parent directory.
di.Delete(True)
End Sub ''Main
End Class ''MoveToTest


这篇关于如何获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆