2012-03-07 56 views
3

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

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); 
     } 
    } 
} 
+0

也许你打算ü se全名,而不是FullPath。 FullPath是一个受保护的字段,它不是以这种方式使用的。有关访问修饰符的说明,请参阅http://msdn.microsoft.com/en-us/library/ms173121.aspx。对于FileSystemInfo的字段/属性,请参考:http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.aspx – 2012-03-07 22:52:18

+0

感谢您的帮助:)我将其更改为FullName,并将“公共静态“到”受保护“。它现在有效。我正在结束这个问题。再次感谢:) – cethint 2012-03-07 22:57:59

+0

你总是可以看到该领域是否受到保护,私人等。按F12。 //总结: //为System.IO.FileInfo和System.IO.DirectoryInfo提供基类//对象。 [序列化] [标记有ComVisible特性(真)] 公共抽象类FileSystemInfo:MarshalByRefObject的,ISerializable的 { //摘要: //表示的目录或文件的完全合格的路径。 保护字符串FullPath; – 2012-03-07 22:59:16

回答

6

你正在寻找的属性被称为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); 
     } 
    } 
} 
+0

谢谢;)它的工作。 – cethint 2012-03-07 23:14:08

相关问题