The use and analysis of the C# operation path (Path) class method

English translation:
Please add image description
As the name suggests, it is the operation path

Namespaces:

using System.IO;

Commonly used in this namespace are file streams... and so on are some classes and methods related to operating files


This article explains and runs all the methods in it...

insert image description here
F12 go to definition found is a static class
Please add image description

What is a static class?
Static classes can create objects without new(Path p =new Path) and can be called directly through the class name (Path.GetFileName())

This is a static class... everything contained in a static class must be static methods


The next step is to introduce the static method inside.

Path class method:

Because it is a class that operates on paths, the paths are all strings, so the return values ​​are basically of string type (string).

Get path filename:

 string str= Path.GetFileName(@"C:\生产队的驴.txt");
 Console.WriteLine(str);

operation result:

Donkey of the production team.txt

The method is to extract the file name in the path including the extension


Absolute path:
that is, the full path

 string   path =Path.GetFullPath(@"C:\temp\生产队的驴.txt");       
 Console.WriteLine(path);

operation result:

C:\temp\The donkey of the production team.txt


Get filename (without extension):

string str= Path.GetFileNameWithoutExtension(@"C:\生产队的驴.txt");
Console.WriteLine(str);

operation result:

production team donkey


Get the file extension:

   string str= Path.GetExtension(@"C:\生产队的驴.txt");
            Console.WriteLine(str);

operation result:

.txt


Get file path:

  string str= Path.GetDirectoryName(@"C:\temp\生产队的驴.txt");
  Console.WriteLine(str);

operation result:

C:\temp

The method contains the folder name but not the filename with extension


Determine a folder or file:

  bool b = Path.HasExtension(@"C:\temp\");
 Console.WriteLine(b);

This method is to determine whether the path is a folder, if it is a folder, return the flase file (txt, mp4, mp3...) and return true

operation result:

flse

not the case of a folder

bool b = Path.HasExtension(@"C:\temp\生产队的驴.txt");
 Console.WriteLine(b);

operation result:

true


Change the path extension:

  string str  = Path.ChangeExtension(@"C:\temp\生产队的驴.txt",".jpg");
            Console.WriteLine(str);

It should be noted that this method does not change the extension of the entity file, but only the extension of the current path.

Running result:
Please add image description
just change the string...the entity file does not change


Merge path:

 string str  = Path.Combine(@"C:\temp\.生产队的驴.txt","测试");
            Console.WriteLine(str);

operation result:

C:\temp.production_team_ass.txt\test

This method is similar to the "+" of strings, that is, the concatenation of two strings is equivalent to

string s = @"\测试";
   string str = @"C:\temp\.生产队
  Console.WriteLine(str + s);

operation result:

C:\temp.production_team_ass.txt\test

It's all the same...


Check if the paths are the same:

bool path =Path.ReferenceEquals(@"C:\temp\生产队的驴.txt", @"C:\temp\生产队的驴.txt");

What is returned is a boolean value if the same True is responsible for the False
running result:

True


Is it the root directory:

   bool  path =Path.IsPathRooted(@"C:\temp\生产队的驴.txt");
  Console.WriteLine(path);

operation result:

True

Can be understood as whether it is a legal path

Incorrect is:

 bool  path =Path.IsPathRooted(@"temp\生产队的驴.txt");
            Console.WriteLine(path);

operation result:

False


Current user temporary file path:

string  path =Path.GetTempPath();
            Console.WriteLine(path);

operation result:

C:\Users\Acer\AppData\Local\Temp\

This path stores files stored by other applications that do not contain important data and can be cleared... Software and system caches, temporary files are stored here


Temporary file with zero bytes:
insert image description here

 string  path =Path.GetTempFileName();
            Console.WriteLine(path);

operation result:

C:\Users\Acer\AppData\Local\Temp\tmp1F56.tmp

Also a temp file...don't know what's the use...
insert image description here


Randomly generate filename and extension:

string  path =Path.GetRandomFileName();
        Console.WriteLine(path);

operation result:

q1bhhhpm.f5s

This can be used for batch saving of files...such as pictures


Get directory current disk:

 string  path =Path.GetPathRoot(@"D:\temp\生产队的驴.txt");
            Console.WriteLine(path);

operation result:

D:\


Path illegal character set:
that is, characters that file names cannot contain, such as (< > "), etc.

  char[]  path =Path.GetInvalidPathChars();
          foreach(char i in path)
            Console.Write(i);

That is, the illegal characters are all in this array.

But I don't know why it is garbled...only a few are displayed...but I feel that this method is useless, a
insert image description here
total of 35

Illegal character set for file:
insert image description here

 char[]  path =Path.GetInvalidFileNameChars();
          foreach(char i in path)
            Console.WriteLine(i);

The same is also the feeling of garbled code, so I didn't study so, a
insert image description here
total of 40


Pure hand play, give a thumbs up~

Guess you like

Origin blog.csdn.net/dpc5201314/article/details/122831723