简体   繁体   English

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

[英]Why can I only write ONE item in my Resource.resx file?

I thought I am writing 10 file extensions and their related Icons as Bitmap into a resource file within a for loop.我以为我正在将 10 个文件扩展名及其相关图标作为 Bitmap 写入 for 循环内的资源文件中。 The odd thing is that only the last file extension with its Icon is written into the Resource.resx file.奇怪的是,只有最后一个带有图标的文件扩展名被写入 Resource.resx 文件。 Somehow the next file extension in the loop is overwriting the previous one, but WHY?不知何故,循环中的下一个文件扩展名覆盖了前一个文件扩展名,但是为什么? I thought a resource is sort of a dictionary with key/value pair where I can add as much as I want just as I do in the Resource designer...我认为资源是一种带有键/值对的字典,我可以在其中添加尽可能多的内容,就像在资源设计器中一样...

What do I wrong?我错了什么?

My code:我的代码:

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;
        }

The problem is here:问题在这里:

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

Each time you create a new writer object and write to it.每次创建一个新的写入器 object 并写入它。 But you don't have the creation of the writer object read from the old file.但是您没有从旧文件中读取写入器 object 的创建。 So you overwrite every time.所以你每次都覆盖。 You should be able to use a different constructor and solve your problem.您应该能够使用不同的构造函数并解决您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM