项目中需要使用到路径处理的地方比较多,对于路径的解析和匹配有时较为繁琐,现在提供一个对路径进行解析的方法:

1.验证设置路径字符串:

        /// <summary>
/// 验证设置路径字符串
/// </summary>
/// <param name="path">路径字符串</param>
/// <param name="isSequential">如果正在创建路径</param>
static public void ValidatePath(string path, bool isSequential)
{
ValidatePath(isSequential ? path + "" : path);
} /// <summary>
/// 验证设置路径字符串
/// </summary>
/// <param name="path">路径字符串</param>
/// <exception cref="ArgumentException">路径无效</exception>
static public void ValidatePath(string path)
{
if (path == null)
throw new ArgumentException("路径不能为空");
if (path.Length == )
throw new ArgumentException("路径长度必须大于0");
if (path[] != '/')
throw new ArgumentException("路径必须启动/字符");
if (path.Length == ) return;
if (path[path.Length - ] == '/')
throw new ArgumentException("路径不能结束与/字符"); string reason = null;
var lastc = '/';
var chars = path.ToCharArray();
for (var i = ; i < chars.Length; lastc = chars[i], i++)
{
var c = chars[i]; if (c == ) { reason = "不允许空字符 @" + i; break; }
if (c == '/' && lastc == '/') { reason = "指定的空节点名称@" + i; break; }
if (c == '.' && lastc == '.')
{
if (chars[i - ] != '/' || ((i + != chars.Length) && chars[i + ] != '/')) continue;
reason = "不允许的相对路径 @" + i;
break;
}
if (c == '.')
{
if (chars[i - ] != '/' || ((i + != chars.Length) && chars[i + ] != '/')) continue;
reason = "不允许的相对路径 @" + i;
break;
}
if ((c <= '\u0000' || c >= '\u001f') && (c <= '\u007f' || c >= '\u009F') &&
(c <= '\ud800' || c >= '\uf8ff') && (c <= '\ufff0' || c >= '\uffff')) continue;
reason = "无效的字符 @" + i;
break;
} if (reason != null) throw new ArgumentException(string.Format("无效的路径字符串 \"{0}\" 引起的 {1}", path, reason));
}

2.查看服务器的路径:

        /// <summary>
/// 在目录到客户端的路径(如果有的话)。期望
///此功能是客户端路径已在此之前验证
////调用/函数调用
/// </summary>
/// <param name="chroot"></param>
/// <param name="clientPath">节点的路径。</param>
/// <returns>查看服务器的路径(chroot添加到客户端的路径)</returns>
static public string PrependChroot(string chroot, string clientPath)
{
if (string.IsNullOrEmpty(chroot)) return clientPath;
return clientPath.Length == ? chroot : string.Concat(chroot, clientPath);
}

3.删除目录:

        /// <summary>
/// 删除目录
/// </summary>
/// <param name="chroot"></param>
/// <param name="serverPath"></param>
/// <returns></returns>
static public string RemoveChroot(string chroot, string serverPath)
{
if (string.IsNullOrEmpty(chroot)) return serverPath;
return string.Compare(serverPath, chroot, StringComparison.Ordinal) == ? "/" : serverPath.Substring(chroot.Length);
}

最新文章

  1. Java--自定义Class并且在内存中编译,加载,实例化
  2. 转-squid介绍及其简单配置
  3. PL/SQL Transaction Control
  4. Java IO流整理Rick
  5. CodeForces 185A 快速幂
  6. hdu----(4545)魔法串(LCS)
  7. CSS打造三级下拉菜单
  8. 用JDBC访问ORACLE数据库 关于commit 增快效率 大数据 等的整理
  9. C#基础之二
  10. Android网络开发之OkHttp--基本用法POST
  11. 为什么字符串会有length属性-JS中包装对象
  12. C# [LINQ] Linq Expression 获取多格式文件
  13. 关于Git的总结
  14. cestOs 7安装Jenkins
  15. PHP反射机制实现自动依赖注入
  16. mysql select 字段别名是否可以用在 select中或者where中
  17. Virut.ce-感染型病毒分析报告
  18. windows socket网络编程资料汇集
  19. Zabbix利用msmtp+mutt发送邮件报警
  20. 小米范工具系列之四:小米范HTTP批量发包器

热门文章

  1. 谁说JavaScript容易?
  2. bzoj3631: [JLOI2014]松鼠的新家(LCA+差分)
  3. int与string类型的转换
  4. Python join()函数
  5. Python黑帽编程3.0 第三章 网络接口层攻击基础知识
  6. ASP.Net MVC开发基础学习笔记:一、走向MVC模式
  7. .NET core for docker
  8. 利用Hexo搭建个人博客-环境搭建篇
  9. zookeeper分布式锁原理
  10. iOS-大神们的博客收集