当前位置: 首页>>代码示例>>C#>>正文


C# Path.NormalizeSlashes方法代码示例

本文整理汇总了C#中System.Management.Path.NormalizeSlashes方法的典型用法代码示例。如果您正苦于以下问题:C# Path.NormalizeSlashes方法的具体用法?C# Path.NormalizeSlashes怎么用?C# Path.NormalizeSlashes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Management.Path的用法示例。


在下文中一共展示了Path.NormalizeSlashes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CalculateFullPath

        public static string CalculateFullPath(Path curLocation, Path changeCommandStr)
        {
            // TODO: sburnicki rewrite that stuff with pathintrinsics
            var changeCommand = (changeCommandStr ?? string.Empty).NormalizeSlashes();
            var currentLocation = curLocation.NormalizeSlashes();

            bool applyParts = false;
            Path resultPath;

            // use the input 'changeCommand' path if it's 
            // 'rooted' otherwise we go from the currentLocation
            if (changeCommand.HasDrive())
            {
                // windows case where changeCommand == "/" or "\" but the currentLocation has a "C:" drive
                string currentLocationDrive = currentLocation.GetDrive();
                if (changeCommand.StartsWithSlash() && !changeCommand.GetDrive().Equals(currentLocationDrive, StringComparison.InvariantCultureIgnoreCase))
                {
                    resultPath = new Path(currentLocation.CorrectSlash, currentLocation.WrongSlash, string.Format("{0}:{1}", currentLocationDrive, changeCommand));
                }
                else
                {
                    resultPath = changeCommand;
                }
            }
            else
            {
                applyParts = true;
                resultPath = currentLocation;
            }

            var correctSeparator = Char.Parse(currentLocation.CorrectSlash);
            var changeParts = changeCommand.ToString().Split(correctSeparator).Where(s => !string.IsNullOrEmpty(s));

            foreach (var part in changeParts)
            {
                // ignore single dot as it does nothing...
                if (part == ".")
                {
                    continue;
                }

                // ignore trying to go up a dir from the root dir
                if (part == ".." && resultPath.IsRootPath())
                {
                    continue;
                }

                if (part == "..")
                {
                    resultPath = resultPath.GetParentPath(currentLocation.GetDrive());
                }
                else if (applyParts)
                {
                    resultPath = resultPath.Combine(part);
                }
            }

            return resultPath.ApplyDriveSlash();
        }
开发者ID:mauve,项目名称:Pash,代码行数:59,代码来源:PathNavigation.cs

示例2: Combine

        public Path Combine(Path child)
        {
            var parent = this;

            if (string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child))
            {
                return child;
            }

            if (string.IsNullOrEmpty(parent) && !string.IsNullOrEmpty(child))
            {
                return child.NormalizeSlashes();
            }

            parent = parent.NormalizeSlashes();

            if (!string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child))
            {
                if (parent.EndsWithSlash())
                {
                    return parent;
                }
                else
                {
                    return parent.AppendSlashAtEnd();
                }
            }

            child = child.NormalizeSlashes();
            var builder = new System.Text.StringBuilder(parent);

            if (!parent.EndsWithSlash())
                builder.Append(CorrectSlash);

            // Make sure we do not add two \
            if (child.StartsWithSlash())
            {
                builder.Append(child, 1, child.Length - 1);
            }
            else
            {
                builder.Append(child);
            }

            return new Path(CorrectSlash, WrongSlash, builder.ToString());
        }
开发者ID:,项目名称:,代码行数:46,代码来源:

示例3: SetLocation

        internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime)
        {
            // TODO: deal with paths starting with ".\"

            if (path == null)
            {
                throw new NullReferenceException("Path can't be null");
            }

            if (path == "~")
            {
                // Older Mono versions (sadly the one that's currently still
                // available) have a bug where GetFolderPath returns an empty
                // string for most SpecialFolder values, but only on
                // non-Windows.
                // See: https://bugzilla.xamarin.com/show_bug.cgi?id=2873

                path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

                // HACK: Use $Env:HOME until Mono 2.10 dies out.
                if (path == "")
                    path = Environment.GetEnvironmentVariable("HOME");
            }

            PSDriveInfo nextDrive = CurrentDrive;

            path = path.NormalizeSlashes();

            string driveName = null;
            if (path.TryGetDriveName(out driveName))
            {
                try
                {
                    nextDrive = _executionContext.SessionState.Drive.Get(driveName);
                }
                catch (MethodInvocationException) //didn't find a drive (maybe it's "\" on Windows)
                {
                    nextDrive = CurrentDrive;
                }
            }

            Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path);

            // I'm not a fan of this block of code.
            // The goal here is to throw an exception if trying to "CD" into an invalid location
            //
            // Not sure why the providerInstances are returned as a collection. Feels like given a
            // path we should have one provider we're talking to.
            if (_providerInstances.ContainsKey(nextDrive.Provider.Name))
            {
                bool pathExists = false;
                IEnumerable<ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast<ItemCmdletProvider>();
                ItemCmdletProvider currentProvider = null;
                foreach (var provider in cmdletProviders)
                {
                    if (provider.ItemExists(newLocation, providerRuntime))
                    {
                        pathExists = true;
                        currentProvider = provider;
                        break;
                    }
                }

                if (!pathExists)
                {
                    throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation));
                }
                else
                {
                    if (currentProvider is FileSystemProvider)
                    {
                        System.Environment.CurrentDirectory = newLocation;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name);
            }

            nextDrive.CurrentLocation = newLocation;

            CurrentDrive = nextDrive;
            _providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive;
            return CurrentLocation;
        }
开发者ID:,项目名称:,代码行数:86,代码来源:

示例4: GetParentPath

        public Path GetParentPath(Path root)
        {
            var path = this;
            // normalize first
            path = path.NormalizeSlashes().TrimEndSlash();

            if (root != null)
            {
                root = root.NormalizeSlashes().TrimEndSlash();
                if (string.Equals(path, root, StringComparison.CurrentCultureIgnoreCase))
                {
                    return new Path(CorrectSlash, WrongSlash, string.Empty);
                }
            }

            int iLastSlash = path._rawPath.LastIndexOf(CorrectSlash);

            string newPath = root;

            if (iLastSlash > 0)
            {
                newPath = path._rawPath.Substring(0, iLastSlash);
            }
            else if (iLastSlash == 0)
            {
                newPath = new Path(CorrectSlash, WrongSlash, CorrectSlash);
            }

            Path resultPath = new Path(CorrectSlash, WrongSlash, newPath);

            return resultPath.ApplyDriveSlash();
        }
开发者ID:mauve,项目名称:Pash,代码行数:32,代码来源:Path.cs

示例5: SetLocation

        internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime)
        {
            // TODO: deal with paths starting with ".\"

            if (path == null)
            {
                throw new NullReferenceException("Path can't be null");
            }

            PSDriveInfo nextDrive = CurrentDrive;

            path = path.NormalizeSlashes();

            string driveName = null;
            if (path.TryGetDriveName(out driveName))
            {
                nextDrive = GetDrive(driveName);
            }

            if (nextDrive == null)
            {
                nextDrive = CurrentDrive;
            }

            Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path);

            // I'm not a fan of this block of code.
            // The goal here is to throw an exception if trying to "CD" into an invalid location
            //
            // Not sure why the providerInstances are returned as a collection. Feels like given a
            // path we should have one provider we're talking to.
            if (_providerInstances.ContainsKey(nextDrive.Provider.Name))
            {
                bool pathExists = false;
                IEnumerable<ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast<ItemCmdletProvider>();
                ItemCmdletProvider currentProvider = null;
                foreach (var provider in cmdletProviders)
                {
                    if (provider.ItemExists(newLocation, providerRuntime))
                    {
                        pathExists = true;
                        currentProvider = provider;
                        break;
                    }
                }

                if (!pathExists)
                {
                    throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation));
                }
                else
                {
                    if (currentProvider is FileSystemProvider)
                    {
                        System.Environment.CurrentDirectory = newLocation;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name);
            }

            nextDrive.CurrentLocation = newLocation;

            CurrentDrive = nextDrive;
            _providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive;
            return CurrentLocation;
        }
开发者ID:b333z,项目名称:Pash,代码行数:69,代码来源:SessionStateGlobal.cs


注:本文中的System.Management.Path.NormalizeSlashes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。