【问题标题】:"'System.IO.FileSystemInfo.FullPath' is inaccessible due to its protection level" error in C#C# 中的“'System.IO.FileSystemInfo.FullPath' 由于其保护级别而无法访问”错误
【发布时间】:2012-03-07 22:48:24
【问题描述】:

我有一个如下所示的 C# 程序。但它失败了。 错误是“System.IO.FileSystemInfo.FullPath”由于其保护级别而无法访问。 FullPath 用蓝色下划线。

protected void Main(string[] args)
{
    DirectoryInfo parent = new DirectoryInfo(@"C:\Users\dell\Desktop\rename");
    foreach (DirectoryInfo child in parent.GetDirectories())
    {
        string newName = child.FullPath.Replace('_', '-');

        if (newName != child.FullPath)
        {
            child.MoveTo(newName);
        }
    }
}

【问题讨论】:

  • 也许您打算使用 FullName,而不是 FullPath。 FullPath 是一个受保护的字段,它不适合以这种方式使用。有关访问修饰符的说明,请参阅 msdn.microsoft.com/en-us/library/ms173121.aspx。对于 FileSystemInfo 的字段/属性,请参阅:msdn.microsoft.com/en-us/library/system.io.filesysteminfo.aspx
  • 感谢您的帮助 :) 我将其更改为 FullName 并将“public static”替换为“protected”。现在可以了。我正在结束这个问题。再次感谢:)
  • 您可以随时查看该字段是否受保护、私有等。按 F12。 // 摘要: // 为 System.IO.FileInfo 和 System.IO.DirectoryInfo // 对象提供基类。 [Serializable] [ComVisible(true)] public abstract class FileSystemInfo : MarshalByRefObject, ISerializable { // 摘要: // 表示目录或文件的完全限定路径。受保护的字符串 FullPath;

标签: c#


【解决方案1】:

您要查找的属性称为FullName,而不是FullPath

static void Main()
{
    DirectoryInfo parent = new DirectoryInfo(@"C:\Users\dell\Desktop\rename");
    foreach (DirectoryInfo child in parent.GetDirectories())
    {
        string newName = child.FullName.Replace('_', '-');

        if (newName != child.FullName)
        {
            child.MoveTo(newName);
        }
    }
}

【讨论】:

    【解决方案2】:

    尝试使用 FullName 而不是 FullPath:

    http://msdn.microsoft.com/fr-fr/library/8s2fzb02.aspx

    这应该适合你:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2011-04-05
      • 2015-09-26
      • 2011-09-01
      相关资源
      最近更新 更多