当前位置:首页 > 开发 > C# > 正文内容

C# System.IO.Path

C#4个月前 (12-23)

System.IO.Path.GetExtension

返回指定的路径字符串的扩展名。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetExtension(file1));
//.mp3

string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetExtension(file2));
//.mp4

System.IO.Path.ChangeExtension

更改路径字符串的扩展名。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.ChangeExtension(file1,".mp5"));
//C:\Users\Administrator\Desktop\temp\12345.mp5

string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.ChangeExtension(file2,".mp5"));
//Users\Administrator\Desktop\temp\12345.mp3.mp5

System.IO.Path.Combine

将多个字符串组合成一个路径。

string file1 = @"C:\Users\Administrator\Desktop\temp\";
string file2 = @"12345.mp3";
Console.WriteLine(System.IO.Path.Combine(file1,file2));
//C:\Users\Administrator\Desktop\temp\12345.mp3


Combine( String[] )

将字符串数组组合成一个路径。
Combine(String, String)将两个字符串组合成一个路径。
Combine(String, String, String)将三个字符串组合成一个路径。
Combine(String, String, String, String)将四个字符串组合成一个路径。


System.IO.Path.GetDirectoryName

返回指定路径字符串的目录信息。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetDirectoryName(file1));
//C:\Users\Administrator\Desktop\temp

string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetDirectoryName(file2));
//Users\Administrator\Desktop\temp


System.IO.Path.GetFileName

返回指定路径字符串的文件名和扩展名。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetFileName(file1));
//12345.mp3

string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetFileName(file2));
//12345.mp3.mp4


System.IO.Path.GetFileNameWithoutExtension

返回不具有扩展名的指定路径字符串的文件名。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file1));
//12345

string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file2));
//12345.mp3

System.IO.Path.GetFullPath

返回指定路径字符串的绝对路径。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetFullPath(file1));
//C:\Users\Administrator\Desktop\temp\12345.mp3

string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetFullPath(file2));
//C:\Users\Administrator\Desktop\temp\12345.mp3.mp4

System.IO.Path.GetPathRoot

获取指定路径的根目录信息。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.GetPathRoot(file1));
//C:\

string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.GetPathRoot(file2));
//C:\

System.IO.Path.GetRandomFileName

返回随机文件夹名或文件名。

Console.WriteLine(System.IO.Path.GetRandomFileName());
//4gi0u4hz.10w

System.IO.Path.GetTempFileName

在磁盘上创建磁唯一命名的零字节的临时文件并返回该文件的完整路径。

Console.WriteLine(System.IO.Path.GetTempFileName());
//C:\Users\Administrator\AppData\Local\Temp\tmp4C97.tmp

System.IO.Path.GetTempPath

返回当前用户的临时文件夹的路径。
Console.WriteLine(System.IO.Path.GetTempPath());
//C:\Users\Administrator\AppData\Local\Temp

System.IO.Path.HasExtension

确定路径是否包括文件扩展名。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.HasExtension(file1));
//True

string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.HasExtension(file2));
//True

string file3 = @"C:\Users\Administrator\Desktop\temp\12345";
Console.WriteLine(System.IO.Path.HasExtension(file3));
//False

System.IO.Path.IsPathRooted

获取一个值,该值指示指定的路径字符串是否包含根。

string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3";
Console.WriteLine(System.IO.Path.IsPathRooted(file1));
//True

string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4";
Console.WriteLine(System.IO.Path.IsPathRooted(file2));
//False

System.IO.Path.GetInvalidPathChars

获取包含不允许在路径名中使用的字符的数组。

foreach (int arg in System.IO.Path.GetInvalidPathChars())
{
    Console.WriteLine(arg);
}

/*
34
60
62
124
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
*/

System.IO.Path.GetInvalidFileNameChars

获取包含不允许在文件名中使用的字符的数组。

foreach (int arg in System.IO.Path.GetInvalidFileNameChars())
{
    Console.WriteLine(arg);
}

/*
34
60
62
124
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
58
42
63
92
47
*/


转载请注明出处。

本文链接:http://www.pythonopen.com/?id=1624

相关文章

C# BackgroundWorker

1.概述BackgroundWorker是一个在 WinForms 应用程序中用于简化在后台线程执行...

C# Browsable(bool)

在编程中(比如常见的 C# 语言在开发 Windows Forms 等应用程序时),Browsabl...

C# 类接口

定义接口是一种抽象类型,它定义了一组方法签名(方法名称、参数列表和返回类型),但没有方法体。接口用于...

C# Byte[]转为Bitmap

在 C# 中,可以使用System.Drawing命名空间下的相关类将byte[]类型的数据转换为B...

C# MemoryStream转为Image

        //...

C# TextRenderer.MeasureText

TextRenderer.MeasureText是System.Windows.Forms命名空间中...