为什么我只能在Resource.resx文件中写一个项目?

时间:2011-06-10 20:07:01

标签: c# .net file resources

我以为我正在将10个文件扩展名及其相关的Icon作为Bitmap写入for循环中的资源文件中。奇怪的是,只有带有Icon的最后一个文件扩展名被写入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 个答案:

答案 0 :(得分:1)

问题在于:

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

每次创建新的编写器对象并写入它。但是您没有从旧文件中读取的writer对象的创建。所以你每次都要覆盖。您应该能够使用不同的构造函数并解决您的问题。

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