how to get file from resources as a stream? (.net) - c#

I've got few files in resources (xsd files) that i use for validating received xml messages. The resource file i use is named AppResources.resx and it contains a file called clientModels.xsd. When i try to use the file like this: AppResources.clientModels, i get a string with the file's content. i would like to get a stream instead.
i do not wish to use assembly.GetManifestResourceStream as i had bad experiences with it (using these streams to archive files with SharpZipLib didn't work for some reason).
is there any other way to do it? i've heard about ResourceManager - is it anything that could help me?

Could you feed the string you get into a System.IO.StringReader, perhaps? That may do what you want. You may also want to check out MemoryStream.

here is the code from the link
//Namespace reference
using System;
using System.Resources;
#region ReadResourceFile
/// <summary>
/// method for reading a value from a resource file
/// (.resx file)
/// </summary>
/// <param name="file">file to read from</param>
/// <param name="key">key to get the value for</param>
/// <returns>a string value</returns>
public string ReadResourceValue(string file, string key)
{
//value for our return value
string resourceValue = string.Empty;
try
{
// specify your resource file name
string resourceFile = file;
// get the path of your file
string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
// create a resource manager for reading from
//the resx file
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
// retrieve the value of the specified key
resourceValue = resourceManager.GetString(key);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
resourceValue = string.Empty;
}
return resourceValue;
}
#endregion
I did not write the code it came from
http://www.dreamincode.net/code/snippet1683.htm
HTH
bones

I have a zip file loaded as a resource, and referencing it directly from the namespace gives me bytes, not a string. Right-click on your file in the resources designer, and change the filetype from text to binary. Then you will get a bytearray, which you could load into a MemoryStream.

Related

Accessing Images from resources.resx file not working with Image.FromFile method

I have an Image array, and I want to set an image in that array to be an image from Resources.resx file, I wrote this code here and gave me the exception that the image wasn't found.
string path = "PeanutToTheDungeon.Properties.Resources.ScaredMeter1"
imageArray[0] = Image.FromFile(path);
Does anyone know how to access local files using a path?
Also, the file does exist and it is named correctly too.
EmbeddedResources are not files, they are binary data encoded into the assembly itself. There are multiple ways to read them, the most common being to use the generated code from the resx (e.g., Resources.MyImageName).
The easiest way is just to do that. Find the .resx file in Visual Studio and look for the Resources.Designer.cs file. Inside that, you will find the generated property "ScaredMeter1", and it will likely be of type Bitmap (which derives from Image). To use it, you would have this code:
imageArray[0] = PeanutToTheDungeon.Properties.Resources.ScaredMeter1;
You can also create the image yourself like this:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
internal static class EmbeddedResourceHelper
{
/// <summary>
/// Creates a new <see cref="System.Drawing.Image"/> by reading a <see cref="Stream"/> from an embedded resource.
/// </summary>
/// <param name="assembly">Assembly containing the embedded resource.</param>
/// <param name="resourceName">Name of the resource.</param>
/// <returns>The created <see cref="Image"/>.</returns>
/// <exception cref="ArgumentNullException">One of the arguments was <c>null</c>.</exception>
/// <exception cref="KeyNotFoundException">A resource named <paramref name="resourceName"/> was not found in <paramref name="assembly"/>.</exception>
public static Image ReadEmbeddedResourceImage(Assembly assembly, string resourceName)
{
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
using Stream stream = assembly.GetManifestResourceStream(resourceName)
?? throw new KeyNotFoundException($"{resourceName} is not a valid resource in {assembly.FullName}");
var image = Image.FromStream(stream);
return image;
}
}

Extract ZIP file to memory (not disk)

I need to extract a zip file to memory (not to the disk). I cannot save it to a directory, even temporarily.
Is there a way to extract a zip file just to memory, and perform "File" functions there?
I can't open the file as a file stream because this doesn't allow me to read the metadata (last write time, attributes, etc). Some but not all the file attributes can be read from zip entry itself but this is insufficient for my purposes.
I've been using:
using (ZipArchive archive = ZipFile.OpenRead(openFileDialog.FileName)) // Read files from the zip file
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if(entry.Name.EndsWith(".txt", StringComparison.InvariantCultureIgnoreCase)) // get .txt file
{
FileStream fs = entry.Open() as FileStream;
}
}
}
Thanks.
The code below presents a way to get the file into memory as an array of strings, but it is unclear as to what file functions you are asking for. Other commenters have mentioned ExternalAttributes, which is OS dependent therefore it is relevant to have more information as to the problem space.
using System;
using System.IO;
using System.IO.Compression;
namespace StackOverflowSampleCode
{
class Program
{
/// <summary>
/// Validate the extension is correct
/// </summary>
/// <param name="entry"></param>
/// <param name="ext"></param>
/// <returns></returns>
static bool validateExtension(ZipArchiveEntry entry, string ext)
{
return entry.Name.EndsWith(
ext,
StringComparison.InvariantCultureIgnoreCase);
}
/// <summary>
/// Convert the entry into an array of strings
/// </summary>
/// <param name="entry"></param>
/// <returns></returns>
static string[] extractFileStrings(ZipArchiveEntry entry)
{
string[] file;
// Store into MemoryStream
using (var ms = entry.Open() as MemoryStream)
{
// Verify we are at the start of the stream
ms.Seek(0, SeekOrigin.Begin);
// Handle the bytes of the memory stream
// by converting to array of strings
file = ms.ToString().Split(
Environment.NewLine, // OS agnostic
StringSplitOptions.RemoveEmptyEntries);
}
return file;
}
static void Main(string[] args)
{
string fileName = "";
using (ZipArchive archive = ZipFile.OpenRead(fileName))
{
foreach (var entry in archive.Entries)
{
// Limit results to files with ".txt" extension
if (validateExtension(entry, ".txt"))
{
var file = extractFileStrings(entry);
foreach (var line in file)
{
Console.WriteLine(line);
}
Console.WriteLine($"Last Write Time: {entry.LastWriteTime}");
Console.WriteLine($"External Attributes: {entry.ExternalAttributes}");
}
}
}
}
}
}

Copy FIlestream to new stream giving empty file

I'm working on converting some files, but I'm having some issues on the 2nd step of this.
Load file from source location
Save file to temp folder
Save converted file to Output location
I have 2 methods for reading the original file, but there is a problem with both of them.
Method 1: The file remains locked (so when something goes wrong, I have to restart the app)
Method 2: The temp file is empty
Anybody got an idea on how to fix one of those problems?
Utilities class
/// <summary>
/// Get document stream
/// </summary>
/// <param name="DocumentName">Input document name</param>
public static Stream GetDocumentStreamFromLocation(string documentLocation)
{
try
{
//ExStart:GetDocumentStream
// Method one: works, but locks file
return File.Open(documentLocation, FileMode.Open, FileAccess.Read);
// Method two: gives empty file on temp folder
using (FileStream fsSource = File.Open(documentLocation, FileMode.Open, FileAccess.Read))
{
var stream = new MemoryStream((int)fsSource.Length);
fsSource.CopyTo(stream);
return stream;
}
//ExEnd:GetDocumentStream
}
catch (FileNotFoundException ioEx)
{
Console.WriteLine(ioEx.Message);
return null;
}
}
/// <summary>
/// Save file in any format
/// </summary>
/// <param name="filename">Save as provided string</param>
/// <param name="content">Stream as content of a file</param>
public static void SaveFile(string filename, Stream content, string location = OUTPUT_PATH)
{
try
{
//ExStart:SaveAnyFile
//Create file stream
using (FileStream fileStream = File.Create(Path.Combine(Path.GetFullPath(location), filename)))
{
content.CopyTo(fileStream);
}
//ExEnd:SaveAnyFile
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I Call the following functions as following:
public static StreamContent Generate(string sourceLocation)
{
// Get filename
var fileName = Path.GetFileName(sourceLocation);
// Create tempfilename
var tempFilename = $"{Guid.NewGuid()}_{fileName}";
// Put file in storage location
Utilities.SaveFile(tempFilename, Utilities.GetDocumentStreamFromLocation(sourceLocation), Utilities.STORAGE_PATH);
// ... More code
}
In order to copy the source file to a temp folder, the easiest way is to use the File.Copy method from the System.IO namespace. Consider the following:
// Assuming the variables have been set as you already had, this creates a copy in the intended location.
File.Copy(documentLocation, filename);
After some further digging. I found out that you can add a property in the File.Open that "fixes" this issue:
return File.Open(documentLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
With the downside that you still can't move / rename the file, but the lock is removed.

Issue with the Resource File Conversion

Here is the requirement:
Migrate the project resource file from one version to another version using Resourcefile converter utility.(ConvertResx)
I have created a simple windows application project. Set the localization property in the design time. In the .resx file localizable property entry has been set with 'MetaData' attribute. During the resource file conversion it convert all the entries in .resx file,but the localizable property attribute set with 'Data' attribute instead of 'Metadata' attribute.
UseResxDataNodes' class placed both the data and metadata attribute entries in the same collection.
ResXResourceReader reader = new ResXResourceReader(path0);
reader.UseResXDataNodes = true;
In order to retrieve the metadata collection entries, i have used the below code
iDictionaryEnumerator7 = reader.GetMetadataEnumerator();
But unable to read the metadata properties (localizable) with 'Metadata' attribute tag. it has been set with 'Data' tag in the resx file after resource file conversion.
Could you please help me on this issue, how to read the metadata attributes (design-time property attributes from the .resx file and migrate the referred assemblies to the latest version and write it into the .resx file ) how to migrate the metadata attribute entries in the .resx files.
Regards,
Sivaguru s
I encountered the same problem trying to write out a resource file to the same format as the original. After some searching and trial/error, I came up with the following solution to properly write out the metadata nodes.
ResXResourceReader readerData = new ResXResourceReader(new StringReader(sw.ToString()));
ResXResourceReader readerMetaData = new ResXResourceReader(new StringReader(sw.ToString()));
//Flag to read nodes as ResXDataNode, instead of key/value pairs, to preserve Comments.
readerData.UseResXDataNodes = true;
ResXResourceWriter writer = new ResXResourceWriter(this.FilePath);
foreach (DictionaryEntry resEntry in readerData)
{
ResXDataNode node = resEntry.Value as ResXDataNode;
if (node != null)
{
DictionaryEntry metaDataEntry;
//Check if node is metadata. The reader does not distinguish between
//data and metadata when UseResXDataNodes flags is set to true.
//http://connect.microsoft.com/VisualStudio/feedback/details/524508/resxresourcereader-does-not-split-data-and-metadata-entries-when-useresxdatanodes-is-true
if (IsMetaData(readerMetaData, resEntry, out metaDataEntry))
{
writer.AddMetadata(metaDataEntry.Key.ToString(), metaDataEntry.Value);
}
else
{
writer.AddResource(node);
}
}
}
writer.Generate(); //write to the file
writer.Close();
readerData.Close();
readerMetaData.Close();
/// <summary>
/// Check if resource data is metadata. If so, return the metadata node.
/// </summary>
/// <param name="metaDataReader"></param>
/// <param name="resEntry"></param>
/// <param name="metaData"></param>
/// <returns></returns>
private static bool IsMetaData(ResXResourceReader metaDataReader, DictionaryEntry resEntry, out DictionaryEntry metaData)
{
IDictionaryEnumerator metadataEnumerator = metaDataReader.GetMetadataEnumerator();
while (metadataEnumerator.MoveNext())
{
if (metadataEnumerator.Entry.Key.Equals(resEntry.Key))
{
metaData = metadataEnumerator.Entry;
return true;
}
}
return false;
}

DotNetZip: Add Files to Dynamically Created Archive Directory

I can't imagine this is hard to do, but I haven't been able to get it to work. I have a files class that just stores the location, directory, and name of the files I want to zip. The files I'm zipping exist on disk so the FileLocation is the full path. ZipFileDirectory doesn't exist on disk. If I have two items in my files list,
{ FileLocation = "path/file1.doc", ZipFileDirectory = #"\", FileName = "CustomName1.doc" },
{ FileLocation = "path/file2.doc", ZipFileDirectory = #"\NewDirectory", FileName = "CustomName2.doc" }
I would expect to see MyCustomName1.doc in the root, and a folder named NewDirectory containing MyCustomName2.doc, but what happens is they both end up in the root using this code:
using (var zip = new Ionic.Zip.ZipFile())
{
foreach (var file in files)
{
zip.AddFile(file.FileLocation, file.ZipFileDirectory).FileName = file.FileName;
}
zip.Save(HttpContext.Current.Response.OutputStream);
}
If I use this:
zip.AddFiles(files.Select(o => o.FileLocation), false, "NewDirectory");
Then it creates the new directory and puts all of the files inside, as expected, but then I lose the ability to use the custom naming with this method, and it also introduces more complexities that the first method would handle perfectly.
Is there a way I can get the first method (AddFile()) to work as I expect?
On further inspection, since posting a comment a few minutes ago, I suspect that setting FileName is erasing the archive path.
Testing confirms this.
Setting the name to #"NewDirectory\CustomName2.doc" will fix the problem.
You can also use #"\NewDirectory\CustomName2.doc"
Not sure if this exactly suites your needs but thought I would share. It is a method that is part of a helper class that I created to make working with DotNetZip a bit easier for my dev team. The IOHelper class is another simple helper class that you can ignore.
/// <summary>
/// Create a zip file adding all of the specified files.
/// The files are added at the specified directory path in the zip file.
/// </summary>
/// <remarks>
/// If the zip file exists then the file will be added to it.
/// If the file already exists in the zip file an exception will be thrown.
/// </remarks>
/// <param name="filePaths">A collection of paths to files to be added to the zip.</param>
/// <param name="zipFilePath">The fully-qualified path of the zip file to be created.</param>
/// <param name="directoryPathInZip">The directory within the zip file where the file will be placed.
/// Ex. specifying "files\\docs" will add the file(s) to the files\docs directory in the zip file.</param>
/// <param name="deleteExisting">Delete the zip file if it already exists.</param>
public void CreateZipFile(ICollection<FileInfo> filePaths, string zipFilePath, string directoryPathInZip, bool deleteExisting)
{
if (deleteExisting)
{
IOHelper ioHelper = new IOHelper();
ioHelper.DeleteFile(zipFilePath);
}
using (ZipFile zip = new ZipFile(zipFilePath))
{
foreach (FileInfo filePath in filePaths)
{
zip.AddFile(filePath.FullName, directoryPathInZip);
}
zip.Save();
}
}

Categories

Resources