2012-05-23 38 views
0

我正在提取ISO,然後從提取的ISO中複製文件夾。問題是提取的文件是隻讀的。我試着從屬性菜單,並從C#中的代碼改變它。都沒有工作。提取的文件總是隻讀的

我用來提取ISO的代碼是另一個問題:extract ISO with winrar automatically with c# or batch

我正在尋找一種方法要麼改變提取ISO的屬性,這樣我可以從它的子文件夾複製,或只更改子文件夾的權限。

在此先感謝

UPDATE

新代碼

string[] folderToName = txtCopyFrom.Text.Split('\\'); 
     string isoName = folderToName[folderToName.Length - 1]; 
     isoName = isoName.Remove(isoName.Length - 4, 4); 

     string copyTestFrom = txtCopyTo.Text + @"\"+ isoName + @"\Test\subTest"; 
     string[] folderName = txtCopyFrom.Text.Split('\\'); 
     string folderTestTo = folderName[folderName.Length - 1]; 
     folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4); 
     string copyTest = txtCopyTo.Text; 
     System.IO.Directory.CreateDirectory(copyTest); 

     DirectoryInfo di = new DirectoryInfo(copyTestFrom); 
     di.Attributes &= ~FileAttributes.ReadOnly; 

     foreach (FileInfo fi in di.GetFiles()) 
     { 
      fi.IsReadOnly = false; 

      string destinationPath = Path.Combine(copyTest, Path.GetFileName(copyTestFrom)); 
      File.Copy(copyTestFrom, destinationPath); 
     } 
     MessageBox.Show("Files Copied"); 

在分測驗中的文件不是隻讀,只有本身的文件夾。

目標路徑進入到C:\用戶\ access denied拋出異常mydocs \ ISODump \分測驗

後,我仍然可以將該文件夾複製手動

更新2

解決方法

找到了一個工作,爲我的目的。 directory.move通過移動文件夾達到我想要的目的,而不是複製它。

感謝

+0

'我試着從屬性菜單中改變它,並從C#中的代碼。都沒有工作。「如果你無法從屬性菜單中改變它,你不能指望它從C#代碼中工作。權限問題?您不是試圖修改只讀驅動器上的文件(例如,安裝的iso)? –

+0

@KooKiz我不認爲這是一個特權問題,因爲在拋出異常之後,如果我進入子文件夾並且訪問每個文件的權限,我就可以做到這一點,然後複製它們,否則訪問將被拒絕。但我想要做的是在提取之後立即完成。 – ELSheepO

+0

所以現在我們知道'訪問被拒絕'異常被拋出。請事先提供所有信息。您的代碼的哪一行觸發異常? –

回答

1

你可以試試這個更改屬性:

foreach (string fileName in System.IO.Directory.GetFiles(path)) 
{ 
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); 

    fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly; 
    // or 
    fileInfo.IsReadOnly = true; 
} 

你可以試試這個將屬性遞歸設置爲所有的子目錄和文件:

DirectoryInfo di = new DirectoryInfo(directorypath); 
public void Recurse(DirectoryInfo directory) 
{ 
    foreach (FileInfo fi in directory.GetFiles()) 
    { 
     fi.IsReadOnly = false; // or true 
    } 

    foreach (DirectoryInfo subdir in directory.GetDirectories()) 
    { 
     Recurse(subdir); 
    } 
} 

Test if user has write access to a folder - Check it, 如果仍然存在問題,我相信解決方案應該使用Windows Shell API

+0

仍然得到上面評論中列出的'拒絕'異常。謝謝強硬,非常感謝 – ELSheepO

+0

它仍然有問題..嘗試使用'win32Api' –

+0

什麼是win32api,我在哪裏得到它? – ELSheepO

1

嘗試使用此代碼

fileInfo.IsReadOnly = False 

,而不是

fileInfo.Attributes = FileAttributes.Normal 

有些事情不對以上代碼: 這些行,考慮到您的意見一個路徑

"C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst" 
                      ^^^^^^^^ 
string folderTestTo = folderName[folderName.Length - 1];    
folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4);    
string copyTestTo = folderTestTo;  

將返回此val UE在copyTestTo:

subt 

然後你做

string destinatationPath = Path.Combine(copyTestTo, 
             Path.GetFileName(copyTestFrom)); 

,這會給後面的東西不可能的,因爲這樣的

"subt\C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst" 

我想你應該在你的代碼仔細檢查這些通道路徑在那裏設置一些斷點並檢查你的變量值

+0

對不起,但這沒有奏效。不過謝謝,非常感謝 – ELSheepO

+0

我的不好。 IsReadOnly = False not True – Steve

+0

我已更新我的回答 – Steve