Unity script interface value Path

foreword

When making games or applications, we often consider storing data locally. But when storing data locally, we need to consider the correctness of the path. We usually use the methods encapsulated in the Path class to achieve this.

Path

The Path class is located in the System.IO; assembly and is mainly used to process strings related to paths.
Methods and fields in Path:

namespace System.IO
{
    [ComVisible(true)]
    public static class Path
    {
        public static readonly char AltDirectorySeparatorChar;
        public static readonly char DirectorySeparatorChar;
        public static readonly char PathSeparator;
        public static readonly char VolumeSeparatorChar;

        public static string ChangeExtension(string path, string extension);
        public static string Combine(string path1, string path2);
        public static string GetDirectoryName(string path);
        public static string GetExtension(string path);
        public static string GetFileName(string path);
        public static string GetFileNameWithoutExtension(string path);
        public static string GetFullPath(string path);
        public static char[] GetInvalidFileNameChars();
        public static char[] GetInvalidPathChars();
        public static string GetPathRoot(string path);
        public static string GetRandomFileName();
        public static string GetTempFileName();
        public static string GetTempPath();
        public static bool HasExtension(string path);
        public static bool IsPathRooted(string path);
    }
}

some common methods

    void Start () {
        string path = "D:/Test/A/B/C.jpg";

        Debug.Log(Path.GetDirectoryName(path));
        Debug.Log(Path.GetFullPath(path));
        Debug.Log(Path.GetExtension(path));
        Debug.Log(Path.GetFileNameWithoutExtension(path));
        Debug.Log(Path.GetFileName(path));
    }

output:

D:/Test/A/B
D:\Test\A\B\C.jpg
.jpg
C
C.jpg

It is better to understand the latter three methods by name, and carefully analyze the first two functions.

Path.GetDirectoryName(path)
gets the contents of the path except the file name and file extension, that is, the path to the folder.

Path.GetFullPath(path)
gets the full path, including file name and file extension, but modified according to the habit of the operating system.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705027&siteId=291194637