C# 为什么我只能在Resource.resx文件中写入一项?
我以为我正在将10个文件扩展名及其相关图标作为位图写入for循环中的一个资源文件中。奇怪的是,只有最后一个带有图标的文件扩展名被写入Resource.resx文件。不知何故,循环中的下一个文件扩展名会覆盖上一个文件扩展名,但为什么?我认为资源是一种具有键/值对的字典,我可以像在资源设计器中一样添加任意多的内容 我错了什么 我的代码:C# 为什么我只能在Resource.resx文件中写入一项?,c#,.net,file,resources,C#,.net,File,Resources,我以为我正在将10个文件扩展名及其相关图标作为位图写入for循环中的一个资源文件中。奇怪的是,只有最后一个带有图标的文件扩展名被写入Resource.resx文件。不知何故,循环中的下一个文件扩展名会覆盖上一个文件扩展名,但为什么?我认为资源是一种具有键/值对的字典,我可以像在资源设计器中一样添加任意多的内容 我错了什么 我的代码: private void AddDocument() { OpenFileDialog fileDialog = new O
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;
}
private void AddDocument()
{
OpenFileDialog fileDialog=新建OpenFileDialog();
fileDialog.Multiselect=true;
DialogResult=fileDialog.ShowDialog();
if(result==DialogResult.OK)
{
对于(int i=0;i将GDI位图转换为wpf BitmapImage之前保存
doc.DocumentTypeImage=gdiImage.ConvertGDIImageToWPFBitmapImage();
}
其他的
{
位图图像wpfImage;
//Filetype为new=>从图标中取出位图
Icon Icon=IconFromFilePath(绝对文件路径);
位图位图=icon.ToBitmap();
wpfImage=bitmap.ConvertGDIImageToWPFBitmapImage();
doc.DocumentTypeImage=wpfImage;
//将位图保存到资源
使用(ResXResourceWriter writer=新的ResXResourceWriter(“TBM.Resource”))
{
writer.AddResource(文件扩展名、位图);
writer.Generate();
}
}
文件列表。添加(文档);
}
_documentService.AddDocumentsToPeriod(DocumentList,_parentId);
}
}
private bool TryIsFileExtensionExisting(字符串文件扩展名,输出位图wpfImage)
{
词典入口条目;
使用(ResXResourceReader=new ResXResourceReader(“TBM.Resource”))
{
entry=reader.Cast()
.Where(x=>x.Key.ToString()
.Equals(文件扩展名、StringComparison.CurrentCultureIgnoreCase))
.FirstOrDefault();
};
wpfImage=entry.Value作为位图;
返回条目。Key!=null;
}
私有图标IconFromFilePath(字符串文件路径)
{
图标结果=空;
尝试
{
结果=Icon.ExtractAssociatedIcon(文件路径);
//“#吞咽并不返回任何内容。您也可以在此处提供默认图标
}
抓住
{
}
返回结果;
}
问题在于:
using (ResXResourceWriter writer = new ResXResourceWriter("TBM.Resource"))
{
writer.AddResource(fileExtension, bitmap);
writer.Generate();
}
每次创建新的writer对象并写入时。但是您没有从旧文件读取writer对象的创建。所以每次都要覆盖。您应该能够使用不同的构造函数来解决您的问题
所以每次我创建一个新的writer实例时,我都会创建一个新的资源文件?这可以解释这种行为。。。因此,我在for循环外创建了writer,但在循环内,我也创建了一个ResXResourceReader来读取一些内容,然后我无法访问该文件两次,这导致了InvalidOperationException,因为该文件已被另一个进程访问。Ctor中的Stream参数对我有什么帮助?如果您想在写入文件的同时读取该文件,我认为没有好的方法可以做到这一点。。。。对我来说,这似乎是一种糟糕的代码味道。好的,问题解决了。如果资源中不存在文件扩展名。Resx我不会像以前那样写入资源文件,而是写入临时字典,在for循环之后,我打开一个ResxResourceWriter并将临时内容转储到真正的资源文件中:)我也不应该在for循环之后使用Generate。。。