【问题标题】:How do I get DirectorySecurity from unc path which contains '.' as localhost?如何从包含“。”的 unc 路径获取 DirectorySecurity作为本地主机?
【发布时间】:2017-11-01 00:14:14
【问题描述】:

首先我想指出,所有需要的访问权限都已获得。

private static IEnumerable<FileSystemAccessRule> GetDirectoryAccessRules(string directoryPath)
{
     AuthorizationRuleCollection rules = Directory.GetAccessControl(directoryPath);

问题是如果directoryPath 包含'.' 作为'localhost' 将抛出异常。 (InvalidOperationException)。如果在'localhost' 上更改了'.',则一切正常。

除了常规的字符串修改之外,有没有什么好的方法可以解决这种情况? Path.Combine 之类的?

【问题讨论】:

    标签: c# .net directory localhost


    【解决方案1】:

    您可以简单地替换目录路径中第一次出现的字符点(。)

     if (directoryPath.Contains('.') && directoryPath.IndexOf('.') != directoryPath.LastIndexOf('.')) 
     {
       var regex = new Regex(Regex.Escape("."));
       directoryPath = regex.Replace(directoryPath, ".", 1);
     }
    

    【讨论】:

    • 好的,我赞成您的回答,但也建议您仔细阅读问题!问题是“除了常规的字符串修改之外,有什么好的方法可以解决这种情况吗?”
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多