c# Path.Combine
Path.Combine:
c#获取当前项目路径 :
//获取包含当前执行的代码的程序集的加载文件的完整路径
var appPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine(appPath); //获取模块的完整路径
string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
Console.WriteLine(path); //获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
var dicPath = System.Environment.CurrentDirectory;
Console.WriteLine(dicPath); //获取程序的基目录
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
Console.WriteLine(basePath); //获取和设置包括该应用程序的目录的名称
string domainPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Console.WriteLine(domainPath);
输出结果:

System.IO.Path.Combine 简单来说,就是合并两个及两个以上的路径字符串。
//两个都是绝对路径,返回后者
string tmp = Path.Combine(@"C:\user", @"D:\test.txt");
Console.WriteLine(tmp);
//如果指定的路径之一是零长度字符串,则该方法返回其他路径。
//当然,两个都是零长度字符串,则返回的就是 string.Empty ;
string tmp1 = Path.Combine("", @"D:\test.txt");
Console.WriteLine(tmp1);
string tmp2 = Path.Combine(@"C:\user", "");
Console.WriteLine(tmp2);
//如果其中一个参数为 null ,会抛出异常(System.ArgumentNullException)
string tmp3 = Path.Combine(@"C:\user", null);
Console.WriteLine(tmp3);
输出结果:

所以下面的代码可以完美的工作:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
结果如下:

从这个例子可以知道,我们不需要考虑arr_pa里面的字符串是不是以”\” 结尾,这的确提供了方便,而且这也是很多人喜欢使用Path.Combine的一个原因,但是仅此而已。
Path.Combine 虽然解决了路径连接问题,但是由于很多人片面的去理解它,所有它非常容易造成错误的应用,要想用好Path.Combine 并非易事,下面我会列举几个实例来说明这点。
第一个:当path2 是相对路径的时候,返回的是path2,path1会被丢弃。
看一下下面的代码:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
你知道这段代码输出什么吗?
这段代码的输出如下:

可以看到对于”/test.txt” 和”\test.txt” ,Path.Combine 认为path2是相对路径,所以直接返回path2.。
第二点:路径是驱动器,返回的结果不正确
public static void Main()
{
string[] arr_pa = { @"c:", @"c:\" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
输出结果是:

可以看到,如果path1 是” C:”的话,那么Path.Combine结果就是不正确的。
第三点:无法连接http路径
除了连接本地路路径之外,有的时候,也需要拼接http链接地址,可惜的是System.IO.Path.Combine却无法拼接http地址。
将arr_pa 修改为
string[] arr_pa = { @"http://www.Test.com/", @"http://www.Test.com" };
结果如下:

在这里就没有什么技巧了,纯粹的死记硬背,
public static string Combine(string path1, string path2);
public static string Combine(string path1, string path2, string path3);
public static string Combine(string path1, string path2, string path3, string path4);
其中第二个后面的所有参数都不能带有 “/” 符号的路径,否则还是会返回最后一个能组成的相对路径的。
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa,"/users", pb));
}
}
输出如下:

如果需要支持路径的话。那么需要自己扩展一下该方法即可。。
public static string CombinePath(this string path1, string path2)
{
if (string.IsNullOrEmpty(path2))
return path1;
var paths = path2.Split(new char[] { '\\', '/' });
foreach (var item in paths.Select(s => s.Trim()).Where(s =>!string.IsNullOrEmpty( s)))
{
path1 = System.IO.Path.Combine(path1, item);
}
return path1;
}
输出结果:

但是还是不能解决Url地址的问题
正是因为上述的几点不足,导致Path.Combine 很难用,这也是有一部分人选择使用String.Format 的原因了。
c# Path.Combine的更多相关文章
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...
- C# Path.Combine 方法的用法
C# Path.Combine 方法的用法 *.注意: string filePath3= Path.Combine(string path1,string path2): 情况一: path2中 ...
- Path.Combine 合并两个路径字符串,会出现的问题
Path.Combine(path1,path2) 1.如果path2字符串,以 \ 或 / 开头,则直接返回 path2
- 基于用Path.Combine的优化
Path.Combine: 什么时候会用到Path.Combine呢?,当然是连接路径字符串的时候! 所以下面的代码可以完美的工作: public static void Main() { strin ...
- 使用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)函数进行路径生成 明明是 ...
- Path.Combine Method
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8#System_IO_P ...
- Path.Combine(
// 获取程序的基目录. var dir1 = System.AppDomain.CurrentDomain.BaseDirectory; // 获取模块的完整路径. var dir2 = Syste ...
- C# Path.Combine 缺陷(http路径用Uri类)
Path.Combine: 什么时候会用到Path.Combine呢?,当然是连接路径字符串的时候! 所以下面的代码可以完美的工作: public static void Main() { strin ...
- [原]System.IO.Path.Combine 路径合并
使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...
随机推荐
- select count(1)和 select count(*)
),其实就是计算一共有多少符合条件的行. 1并不是表示第一个字段,而是表示一个固定值. 其实就可以想成表中有这么一个字段,这个字段就是固定值1,),就是计算一共有多少个1. 同理,),也可以,得到的值 ...
- web 视频播放器clappr 相关
https://github.com/tjenkinson/clappr-thumbnails-plugin/ https://github.com/andrefilimono/clappr-flvj ...
- 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1。 buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割
实验内容(mapReduce安装请按照林子雨教程http://dblab.xmu.edu.cn/blog/631-2/) 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为 ...
- spark集成kerberos
1.生成票据 1.1.创建认证用户 登陆到kdc服务器,使用root或者可以使用root权限的普通用户操作: # kadmin.local -q “addprinc -randkey spark/yj ...
- 【洛谷】P5024 保卫王国 (倍增)
前言 传送门 很多人写了题解了,我就懒得写了,推荐一篇博客 那就分享一下我的理解吧(说得好像有人看一样 对于每个点都只有选与不选两种情况,所以直接用倍增预处理出来两种情况的子树之内,子树之外的最值,最 ...
- 【2019.11.18】SDN阅读作业
为什么需要SDN?SDN特点? 随着网络的快速发展,传统互联网出现了如传统网络配置复杂度高等诸多问题,这些问题说明网络架构需要革新,可编程网络的相关研究为 SDN 的产生提供了可参考的理论依据 SDN ...
- ArrayList: java之ArrayList详细介绍(转)
1 ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List ...
- Vintage、滚动率、迁移率的应用
python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...
- elementui---日期格式的选择
在用elementui做数据提交的时候,默认的时间格式一个对象,好麻烦,主要对时间进行格式限制,具体方法如下: <el-form-item :label="$t('oneCard.bi ...
- linux中环境变量和系统加载环境变量的顺序
一.系统环境变量: /etc/profile :这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, uma ...