2012-03-07 55 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

相關問題