在vb中获取项目中文件的路径

时间:2009-04-22 07:59:07

标签: .net vb.net file-io

我在vb.net中运行Windows应用程序。我在名为XmlFiles的文件夹中有一个名为mail.xml的xml文件。默认情况下,我必须获取mail.xml的路径。我要写的代码是什么使它成为可能?

1 个答案:

答案 0 :(得分:5)

您可以使用Path类的GetFullPath静态方法:

Dim fileName As string = "myfile.ext"
Dim path1 As string = "mydir"
Dim path2 As string = "\mydir"
Dim fullPath As string

fullPath = Path.GetFullPath(path1)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path1, fullPath)

fullPath = Path.GetFullPath(fileName)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    fileName, fullPath)

fullPath = Path.GetFullPath(path2)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path2, fullPath)

' Output is based on your current directory, except
' in the last case, where it is based on the root drive
' GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
' GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
' GetFullPath('\mydir') returns 'C:\mydir'

示例来自MSDN

另一种方法是使用FileInfo类的fullName属性(继承自FileSystemInfo),请参阅下面的用法:

Sub DisplayFileSystemInfoAttributes(ByVal fsi As FileInfo)
    ' Assume that this entry is a file.
    Dim entryType As String = "File"

    ' Determine if this entry is really a directory.
    If fsi.Attributes = FileAttributes.Directory Then
        entryType = "Directory"
    End If

    ' Show this entry's type, name, and creation date.
    Console.WriteLine("{0} entry {1} was created on {2:D}", _
        entryType, **fsi.FullName**, fsi.CreationTime)
End Sub

也来自MSDN