Use and analysis of C# operation path class method

Posted by delassus on Sun, 13 Feb 2022 15:25:01 +0100

English Translation:

As the name suggests, it is the operation path

Namespace:

using System.IO;

File stream... And so on are common in this namespace. They are all classes and methods related to operation files

This article explains and runs all the methods inside


F12 go to the definition and find that it is a static class

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

This is static class... All static methods contained in static class must be static methods

The next step is to introduce the static methods inside

Path class method:

Because it is the class of operation path, and the paths are strings, the return values are basically of string type

Get path file name:

 string str= Path.GetFileName(@"C:\Donkey of production team.txt");
 Console.WriteLine(str);

Operation results:

The donkey of the production team txt

This method is to extract the file name in the path, including the extension

Absolute path:
That is, the complete path

 string   path =Path.GetFullPath(@"C:\temp\Donkey of production team.txt");       
 Console.WriteLine(path);

Operation results:

C:\temp \ donkey of the production team txt

Get file name (without extension):

string str= Path.GetFileNameWithoutExtension(@"C:\Donkey of production team.txt");
Console.WriteLine(str);

Operation results:

Donkey of production team

Get file extension:

   string str= Path.GetExtension(@"C:\Donkey of production team.txt");
            Console.WriteLine(str);

Operation results:

.txt

Get file path:

  string str= Path.GetDirectoryName(@"C:\temp\Donkey of production team.txt");
  Console.WriteLine(str);

Operation results:

C:\temp

This method contains the folder name but not the file name with the extension

Judging folders or files:

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

This method is to judge whether the path is a folder. If it is a folder, it returns the flash file (txt,mp4,mp3...) and returns true

Operation results:

flse

Not a folder

bool b = Path.HasExtension(@"C:\temp\Donkey of production team.txt");
 Console.WriteLine(b);

Operation results:

true

Change path extension:

  string str  = Path.ChangeExtension(@"C:\temp\Donkey of production team.txt",".jpg");
            Console.WriteLine(str);

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

Operation results:

It's just that the string is changed... The entity file does not change

Merge path:

 string str  = Path.Combine(@"C:\temp\.Donkey of production team.txt","test");
            Console.WriteLine(str);

Operation results:

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

This method is similar to the "+" of a string, that is, two strings are connected and equal to

string s = @"\test";
   string str = @"C:\temp\.production team
  Console.WriteLine(str + s);

Operation results:

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

It's the same, almost the same

Determine whether the paths are the same:

bool path =Path.ReferenceEquals(@"C:\temp\Donkey of production team.txt", @"C:\temp\Donkey of production team.txt");

The returned value is a Boolean value. If it is the same, True is False
Operation results:

True

Root directory:

   bool  path =Path.IsPathRooted(@"C:\temp\Donkey of production team.txt");
  Console.WriteLine(path);

Operation results:

True

It can be understood as whether the path is legal or not

Incorrect:

 bool  path =Path.IsPathRooted(@"temp\Donkey of production team.txt");
            Console.WriteLine(path);

Operation results:

False

Current user temporary file path:

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

Operation results:

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

This path stores the files stored by other applications and does not contain important data. You can clear the cache of... Software and system. Temporary files are stored here

Zero byte temporary file:

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

Operation results:

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

It's also a temporary file... I don't know what's the use

Randomly generated file name and extension:

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

Operation results:

q1bhhhpm.f5s

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

Get directory current disk:

 string  path =Path.GetPathRoot(@"D:\temp\Donkey of production team.txt");
            Console.WriteLine(path);

Operation results:

D:\

Path illegal character set:
That is, the characters that the file name cannot contain, such as (< > "), and so on

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

Illegal characters are all in this array

But I don't know why it's garbled... Only a few are displayed... But I feel this method is useless

35 in total

File illegal character set:

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

It's also the feeling of random code. There's no such research

A total of 40

Pure hand fight, point a praise~

Topics: C# Windows Back-end .NET