System.IO.Path.GetPathRoot C# (CSharp) Méthode

GetPathRoot() public static méthode

public static GetPathRoot ( string path ) : string
path string
Résultat string
        public static string GetPathRoot(string path)
        {
            if (path == null) return null;
            PathInternal.CheckInvalidPathChars(path);

            // Need to return the normalized directory separator
            path = PathInternal.NormalizeDirectorySeparators(path);

            int pathRoot = PathInternal.GetRootLength(path);
            return pathRoot <= 0 ? string.Empty : path.Substring(0, pathRoot);
        }

Usage Example

        /// <summary>
        /// 用路徑抓出使用者電腦容量
        /// </summary>
        /// <param name="Drive">路徑</param>
        /// <returns></returns>
        bool RemainingSpace(string Drive)
        {
            try
            {
                if (Directory.Exists(Drive) == false)
                {
                    Directory.CreateDirectory(Drive);
                }

                string str     = Path.GetPathRoot(Drive);
                bool   success = GetDiskFreeSpaceEx(Path.GetPathRoot(Drive), out ulong FreeBytesAvailable, out ulong TotalNumberOfBytes, out ulong TotalNumberOfFreeBytes);

                if (!success)
                {
                    Inteware_Messagebox Msg = new Inteware_Messagebox();
                    Msg.ShowMessage(TranslationSource.Instance["CannnotGetRemainingSpace"]);
                    return(false);
                }

                label_AvailableSpace.Tag     = TotalNumberOfFreeBytes;
                label_AvailableSpace.Content = ConvertDiskUnit(TotalNumberOfFreeBytes, (int)_diskUnit.MB);
                return(true);
            }
            catch (Exception ex)
            {
                Inteware_Messagebox Msg = new Inteware_Messagebox();
                Msg.ShowMessage(ex.Message);
                return(false);
            }
        }
All Usage Examples Of System.IO.Path::GetPathRoot