【发布时间】: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#