使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法

对它的功能有点不放心,原方法实现如下:

// System.IO.Path
/// <summary>Combines two path strings.</summary>
/// <returns>A string containing the combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If <paramref name="path2" /> contains an absolute path, this method returns <paramref name="path2" />.</returns>
/// <param name="path1">The first path. </param>
/// <param name="path2">The second path. </param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="path1" /> or <paramref name="path2" /> contain one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="path1" /> or <paramref name="path2" /> is null. </exception>
/// <filterpriority></filterpriority>
public static string Combine(string path1, string path2)
{
if (path1 == null || path2 == null)
{
throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
}
Path.CheckInvalidPathChars(path1);
Path.CheckInvalidPathChars(path2);
if (path2.Length == )
{
return path1;
}
if (path1.Length == )
{
return path2;
}
if (Path.IsPathRooted(path2))
{
return path2;
}
char c = path1[path1.Length - ];
// if (c != '\\' && c != '/' && c != ':')
if (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar && c != Path.VolumeSeparatorChar)
{
// return path1 + '\\' + path2;
return path1 + Path.DirectorySeparatorChar + path2;
}
return path1 + path2;
}

方法中合并文件路径没问题 C:\\\\123.txt 但若是合并网址就会像这样 http://www.baidu.com\\search

提外话: 这路径在 谷歌浏览器 和 IE10 都被正确识别为 http://www.baidu.com/search

但还是觉得不够完美

[原]System.IO.Path.Combine 路径合并的更多相关文章

  1. 使用System.IO.Combine(string path1, string path2, string path3)四个参数的重载函数提示`System.IO.Path.Combine(string, string, string, string)' is inaccessible due to its protection level

    今天用Unity5.5.1开发提取Assets目录的模块,使用时采用System.IO.Path.Combine(string, string, string, string)函数进行路径生成 明明是 ...

  2. System.IO.Path文件路径类

    Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...

  3. System.IO.Path类

    System.IO.Path为路径的操作封装了很多很有的东西,利用该类提供的方法能够快速处理路径操作的问题.下面详细了解一下. 1.属性 属性太复杂了,反映什么系统平台的信息,看不懂,等以后看得懂了再 ...

  4. C#使用System.IO.Path获取文件路径、文件名

    class Program { static void Main(string[] args) { //获取当前运行程序的目录 string fileDir = Environment.Current ...

  5. System.IO.Path 文件名、路径、扩展名 处理

    string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...

  6. System.IO.Path 文件名、路径、扩展名处理

    string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...

  7. C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

    一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...

  8. System.IO.Path

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  9. System.IO.Path 操作

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

随机推荐

  1. JS底层知识理解之执行上下文篇

    JS底层知识理解之执行上下文篇 一.什么是执行上下文(Execution Context) 执行上下文可以理解为当前代码的执行环境,它会形成一个作用域. 二.JavaScript引擎会以什么方式去处理 ...

  2. the usage of String

    经典算法:(整理汇总) 1)如何快速倒叙: public static char[] reverseArray(char[] c){ char t; for(int i=0;i<c.length ...

  3. NBUT 1221 Intermediary 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB It is widely known that any two strangers can get to know ...

  4. App Transfer:苹果允许iOS App从一个开发者帐号转至另一个开发者账号

    App Transfer:苹果允许iOS App从一个开发者帐号转至另一个开发者账号 苹果在WWDC上宣布超过30万的开发者为iOS平台开发超过90万的应用,你可能会想到有人想出售或者购买app. 现 ...

  5. caffe测试mnist问题-error while loading shared libraries

    问题描述 build/examples/mnist/convert_mnist_data.bin: error while loading shared libraries: libcudart.so ...

  6. 一步步入门log4cpp

    前言 项目实现过程中,需要检查.查找或者调试程序bug等,此时程序日志则较为清晰地展现代码的运行过程.目前接触到的方法有打印消息到控制台,将重要信息输出到某个文件比如txt文件,或者直接使用日志库. ...

  7. 连接APB1和APB2的设备有哪些

    在STM32中 APB1(低速外设)上的设备有:电源接口.备份接口.CAN.USB.I2C1.I2C2.UART2.UART3.SPI2.窗口看门狗.Timer2.Timer3.Timer4 . AP ...

  8. 古典、SOA、传统、K8S、ServiceMesh

    古典.SOA.传统.K8S.ServiceMesh 十几年前就有一些公司开始践行服务拆分以及SOA,六年前有了微服务的概念,于是大家开始思考SOA和微服务的关系和区别.最近三年Spring Cloud ...

  9. PyCharm首次使用(Hell world!)

    作为PyCharm编辑器的起步,我们理所当然的先写一个Hello word,并运行它.(此文献给对IDE不熟悉的初学者) 1,新建一个项目 File --> New Project... 2,新 ...

  10. POJ3276(遍历+区间修改)

    http://poj.org/problem?id=3276 题意:n(n<=5000)头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全 ...