示例#1
0
        private System.IO.DriveInfo GetDrive()
        {
            string root;

            if (Platform.IsClientPosix)
            {
                string path = Util.AppendDirSeparator(systemIO.PathGetFullPath(m_path));
                root = "/";

                //Find longest common prefix from mounted devices
                //TODO: Can trick this with symlinks, where the symlink is on one mounted volume,
                // and the actual storage is on another
                foreach (System.IO.DriveInfo di in System.IO.DriveInfo.GetDrives())
                {
                    if (path.StartsWith(Util.AppendDirSeparator(di.Name), StringComparison.Ordinal) && di.Name.Length > root.Length)
                    {
                        root = di.Name;
                    }
                }
            }
            else
            {
                root = systemIO.GetPathRoot(m_path);
            }

            // On Windows, DriveInfo is only valid for lettered drives. (e.g., not for UNC paths and shares)
            // So only attempt to get it if we aren't on Windows or if the root starts with a letter.
            if (!Platform.IsClientWindows || (root.Length > 0 && char.IsLetter(root[0])))
            {
                try
                {
                    return(new System.IO.DriveInfo(root));
                }
                catch (ArgumentException)
                {
                    // If there was a problem, fall back to returning null
                }
            }

            return(null);
        }