Path.Combine Method
public static string Combine (string path1, string path2);
Returns
The combined paths.
If one of the specified paths is a zero-length string, this method returns the other path.
If path2
contains an absolute path, this method returns path2
.
如果不希望path2是绝对路径的话,可以用Path.IsPathRooted函数检查
A rooted path is file path that is fixed to a specific drive or UNIC path; it contrasts with a path that is relative to the current drive or working directory.
For example, on Windows systems, a rooted path begins with a backslash (for example, "\Documents") or a drive letter and colon (for example, "C:Documents").
Note that rooted paths can be either absolute (that is, fully qualified) or relative.
An absolute rooted path is a fully qualified path from the root of a drive to a specific directory.
A relative rooted path specifies a drive, but its fully qualified path is resolved against the current directory.
The following example illustrates the difference.
using System;
using System.IO; class Program
{
static void Main()
{
string relative1 = "C:Documents";
ShowPathInfo(relative1); string relative2 = "/Documents";
ShowPathInfo(relative2); string absolute = "C:/Documents";
ShowPathInfo(absolute);
} private static void ShowPathInfo(string path)
{
Console.WriteLine($"Path: {path}");
Console.WriteLine($" Rooted: {Path.IsPathRooted(path)}");
Console.WriteLine($" Fully qualified: {Path.IsPathFullyQualified(path)}");
Console.WriteLine($" Full path: {Path.GetFullPath(path)}");
Console.WriteLine();
}
}
// The example displays the following output when run on a Windows system:
// Path: C:Documents
// Rooted: True
// Fully qualified: False
// Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
//
// Path: /Documents
// Rooted: True
// Fully qualified: False
// Full path: c:\Documents
//
// Path: C:/Documents
// Rooted: True
// Fully qualified: True
// Full path: C:\Documents
最新文章
- python-切片 迭代 生成器
- 2015ACM/ICPC亚洲区长春站
- Font Awesome图标库
- XMPP客户端开发(2)--发送接收消息
- iOS中多线程原理与runloop介绍
- MyBatis增删改查
- Ruby Scripting - Array
- 支持 IE8 IE11 和 FF, Chrome 浏览器的圆角
- Hibernate4.1.4配置二级缓存EHCache步骤
- Cube Stacking(并差集深度+结点个数)
- [原创] JavaScript实现简单的颜色类标签云
- 201521123084 《Java程序设计》第10周学习总结
- 201521123020 《Java程序设计》第6周学习总结
- mac 上格式化磁盘出现MediaKit报告设备上的空间不足以执行请求的解决办法
- 微信公众号使用LocalStorage解决返回缓存问题
- 全网最详细的大数据集群环境下多个不同版本的Cloudera Hue之间的界面对比(图文详解)
- OpenStack 图形化服务 Horizon介绍和部署(十二)
- 由一次 symbol lookup error 引发的思考
- Git 执行更改
- Throwable vs Exception