Path类 - asp.net 教程-
时间:2010-08-27 来源:星空3
Path 类
对包含文件或目录路径信息的 String 实例执行操作。这些操作是以跨平台的方式执行的。
命名空间:System.IO
程序集:mscorlib(在 mscorlib.dll 中)
.NET Framework 不支持通过由设备名称构成的路径(如“\\.\PHYSICALDRIVE0”)直接访问物理磁盘。
路径是提供文件或目录位置的字符串。路径不必指向磁盘上的位置;例如,路径可以映射到内存中或设备上的位置。路径的准确格式是由当前平台确定的。例如,在某些系统上,路径可以驱动器号或卷号开始,而此元素在其他系统中是不存在的。在某些系统上,文件路径可以包含扩展名,扩展名指示在文件中存储的信息的类型。文件扩展名的格式是与平台相关的;例如,某些系统将扩展名的长度限制为 3 个字符,而其他系统则没有这样的限制。当前平台还确定用于分隔路径中各元素的字符集,以及确定在指定路径时不能使用的字符集。因为这些差异,所以 Path 类的字段以及 Path 类的某些成员的准确行为是与平台相关的。
路径可以包含绝对或相对位置信息。绝对路径完整指定一个位置:文件或目录可被唯一标识,而与当前位置无关。相对路径指定部分位置:当定位用相对路径指定的文件时,当前位置用作起始点。若要确定当前目录,请调用 Directory.GetCurrentDirectory。
Path 类的大多数成员不与文件系统交互,并且不验证路径字符串指定的文件是否存在。修改路径字符串的 Path 类成员(例如 ChangeExtension)对文件系统中文件的名称没有影响。但 Path 成员确实验证指定路径字符串的内容;并且如果字符串包含在路径字符串中无效的字符(如 InvalidPathChars 中的定义),则引发 ArgumentException。例如,在基于 Windows 的桌面平台上,无效路径字符可能包括引号 (")、小于号 (<)、大于号 (>)、管道符号 (|)、退格 (\b)、空 (\0) 以及从 16 到 18 和从 20 到 25 的 Unicode 字符。
Path 类的成员使您可以快速方便地执行常见操作,例如确定文件扩展名是否是路径的一部分,以及将两个字符串组合成一个路径名。
Path 类的所有成员都是静态的,因此无需具有路径的实例即可被调用。
Server.MapPath() ?? 'ASP 0175 : 80004005' ??????Path ???nt> /zl/eWebEditor/asp/upload.asp??? 313 ? MapPath ??Path ???????'?? ">注意 |
在接受路径作为输入字符串的成员中,路径必须是格式良好的,否则将引发异常。例如,如果路径是完全限定的但以空格开头,则路径在类的方法中不会被修剪。因此,路径的格式不正确,并将引发异常。同样,路径或路径的组合不能被完全限定两次。例如,“c:\temp c:\windows”在大多数情况下也将引发异常。在使用接受路径字符串的方法时,请确保路径是格式良好的。 |
在接受路径的成员中,路径可以是指文件或仅是目录。指定路径也可以是相对路径或者服务器和共享名称的统一命名约定 (UNC) 路径。例如,以下都是可接受的路径:
· C# 中的“c:\\MyDir\\MyFile.txt”或 Visual Basic 中的“c:\MyDir\MyFile.txt”。
· C# 中的“c:\\MyDir”或 Visual Basic 中的“c:\MyDir”。
· C# 中的“MyDir\\MySubdir”或 Visual Basic 中的“MyDir\MySubDir”。
· C# 中的“\\\\MyServer\\MyShare”或 Visual Basic 中的“\\MyServer\MyShare”。
因为所有这些操作都是对字符串执行的,所以不可能验证结果是否在所有方案中都有效。例如,GetExtension 方法分析您传递给它的字符串,并且从该字符串返回扩展名。但是,这并不意味着在磁盘上存在具有该扩展名的文件。
有关使用此类的示例,请参见下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
检索文件扩展名。 |
GetExtension |
检索文件的完全限定路径。 |
|
检索路径中的文件名和扩展名。 |
|
只检索路径中的文件名。 |
|
只检索路径中的目录名。 |
|
更改文件扩展名。 |
ChangeExtension |
确定目录是否存在。 |
|
确定文件是否存在。 |
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? "> 示例
下面的代码示例演示 Path 类的某些主要成员。
Visual Basic
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? ">复制代码
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path1 As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp\MyTest"
Dim path3 As String = "temp"
If Path.HasExtension(path1) Then
Console.WriteLine("{0} has an extension.", path1)
End If
If Path.HasExtension(path2) = False Then
Console.WriteLine("{0} has no extension.", path2)
End If
If Path.IsPathRooted(path3) = False Then
Console.WriteLine("The string {0} contains no root information.", path3)
End If
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3))
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath())
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName())
' This code produces output similar to the following:
' c:\temp\MyTest.txt has an extension.
' c:\temp\MyTest has no extension.
' The string temp contains no root information.
' The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
End Sub
End Class
C#
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? ">复制代码
using System;
using System.IO;
class Test
{
public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp";
if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
}
if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
}
if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName());
/* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}
C++
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? ">复制代码
using namespace System;
using namespace System::IO;
int main()
{
String^ path1 = "c:\\temp\\MyTest.txt";
String^ path2 = "c:\\temp\\MyTest";
String^ path3 = "temp";
if ( Path::HasExtension( path1 ) )
{
Console::WriteLine( "{0} has an extension.", path1 );
}
if ( !Path::HasExtension( path2 ) )
{
Console::WriteLine( "{0} has no extension.", path2 );
}
if ( !Path::IsPathRooted( path3 ) )
{
Console::WriteLine( "The string {0} contains no root information.", path3 );
}
Console::WriteLine( "The full path of {0} is {1}.", path3, Path::GetFullPath( path3 ) );
Console::WriteLine( "{0} is the location for temporary files.", Path::GetTempPath() );
Console::WriteLine( "{0} is a file available for use.", Path::GetTempFileName() );
Console::WriteLine( "\r\nThe set of invalid characters in a path is:" );
Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" );
Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator();
while ( myEnum->MoveNext() )
{
Char c = *safe_cast<Char^>(myEnum->Current);
Console::WriteLine( c );
}
}
J#
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? ">复制代码
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
String path1 = "c:\\temp\\MyTest.txt";
String path2 = "c:\\temp\\MyTest";
String path3 = "temp";
if (Path.HasExtension(path1)) {
Console.WriteLine("{0} has an extension.", path1);
}
if (!(Path.HasExtension(path2))) {
Console.WriteLine("{0} has no extension.", path2);
}
if (!(Path.IsPathRooted(path3))) {
Console.WriteLine("The string {0} contains no root information.",
path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3,
Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.",
Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.",
Path.GetTempFileName());
Console.WriteLine("\r\nThe set of invalid characters in a path is:");
Console.WriteLine("(Note that the wildcard characters '*' and '?' "
+ "are not invalid.):");
char c = ' ';
for (int iCtr = 0; iCtr < Path.InvalidPathChars.get_Length(); iCtr++) {
c = Path.InvalidPathChars[iCtr];
Console.WriteLine(c);
}
} //main
} //Test
Server.MapPath() ?? 'ASP 0175 : 80004005'
??????Path ???nt>
/zl/eWebEditor/asp/upload.asp??? 313
? MapPath ??Path ???????'?? "> 继承层次结构
System.Object
System.IO.Path