2011-06-10 20 views
0

我以爲我正在將10個文件擴展名及其相關圖標作爲位圖寫入for循環中的資源文件中。奇怪的是,只有帶有Icon的最後一個文件擴展名被寫入Resource.resx文件。不知何故,循環中的下一個文件擴展名將覆蓋前一個文件擴展名,但爲什麼?我認爲資源是一種帶有鍵/值對的字典,我可以像在資源設計器中一樣添加儘可能多的字典...爲什麼我只能在Resource.resx文件中寫入一個項目?

我該怎麼做?

我的代碼:

private void AddDocument() 
     { 
      OpenFileDialog fileDialog = new OpenFileDialog(); 
      fileDialog.Multiselect = true; 

      DialogResult result = fileDialog.ShowDialog(); 
      if (result == DialogResult.OK) 
      { 
       for (int i = 0; i < fileDialog.FileNames.Length; i++) 
       { 
        string absoluteFilePath = fileDialog.FileNames.GetValue(i).ToString(); 
        byte[] file = File.ReadAllBytes(absoluteFilePath); 
        String fileExtension = Path.GetExtension(absoluteFilePath); 

        Bitmap gdiImage; 

        Document doc = new Document(); 
        doc.DocumentData = file; 
        doc.DocumentName = fileDialog.SafeFileNames.GetValue(i).ToString(); 


        if (TryIsFileExtensionExisting(fileExtension, out gdiImage)) 
        { 
         // Filetype was saved before => Convert GDI Bitmap to wpf BitmapImage 
         doc.DocumentTypeImage = gdiImage.ConvertGDIImageToWPFBitmapImage(); 
        } 
        else 
        { 
         BitmapImage wpfImage; 
         // Filetype is new => get Bitmap out of the Icon 
         Icon icon = IconFromFilePath(absoluteFilePath); 
         Bitmap bitmap = icon.ToBitmap(); 
         wpfImage = bitmap.ConvertGDIImageToWPFBitmapImage(); 
         doc.DocumentTypeImage = wpfImage; 

         // Save bitmap to resource 
         using (ResXResourceWriter writer = new ResXResourceWriter("TBM.Resource")) 
         { 
          writer.AddResource(fileExtension, bitmap); 
          writer.Generate(); 
         } 
        }      

        DocumentList.Add(doc); 
       } 
       _documentService.AddDocumentsToPeriod(DocumentList, _parentId); 
      } 
     } 

     private bool TryIsFileExtensionExisting(String fileExtension, out Bitmap wpfImage) 
     { 
      DictionaryEntry entry; 
      using (ResXResourceReader reader = new ResXResourceReader ("TBM.Resource")) 
      { 
       entry = reader.Cast<DictionaryEntry>() 
             .Where(x => x.Key.ToString() 
             .Equals(fileExtension, StringComparison.CurrentCultureIgnoreCase)) 
             .FirstOrDefault(); 
      };    

      wpfImage = entry.Value as Bitmap; 
      return entry.Key != null; 
     } 

     private Icon IconFromFilePath(string filePath) 
     { 
      Icon result = null; 
      try 
      { 
       result = Icon.ExtractAssociatedIcon(filePath); 
       //'# swallow and return nothing. You could supply a default Icon here as well 
      } 
      catch 
      { 
      } 
      return result; 
     } 

回答

1

的問題是在這裏:

 using (ResXResourceWriter writer = new ResXResourceWriter("TBM.Resource")) 
     { 
      writer.AddResource(fileExtension, bitmap); 
      writer.Generate(); 
     } 

每次創建一個新的作家對象和寫入。但是您沒有從舊文件中讀取作者對象的創建。所以你每次都要重寫。你應該能夠使用不同的構造函數並解決你的問題。

http://msdn.microsoft.com/en-us/library/system.resources.resxresourcewriter.aspx

+0

所以每次我創建一個新的作家比如我創建一個新的資源文件?這將解釋行爲...因此,我創建了for循環外的作家,但後來我有問題,在循環內我也創建一個ResXResourceReader讀取一些東西,然後我可以ot訪問該文件兩次導致InvalidOperationException因爲該文件已被另一個進程訪問。 Ctor中的Stream參數如何幫助我? – msfanboy 2011-06-10 20:38:55

+0

我不認爲有一種好方法可以做到這一點,如果你想在寫入文件的同時從文件中讀取....這對我來說似乎是一種不好的代碼味道。 – Hogan 2011-06-10 20:49:21

+0

好問題解決了。如果Resource.Resx中不存在文件擴展名,我不會像之前那樣寫入Resource文件,而是寫入臨時Dictionary ,並且在for循環之後,我打開一個ResxResourceWriter並將臨時內容轉儲到真正的資源文件:)我也不應該使用for循環之後的生成... – msfanboy 2011-06-10 21:36:38

相關問題