0

I would like to use the resources in my solution by passing a string variable to Properties.Resources:

string[] documents = new string[] { "one", "two", "three"};
foreach (var document in documents)
{
   extractFile(String.Format(@"C:\temp\{0}.doc",document), properties.Resources.document);
}

private void extractFile (string destinationFile, byte[] sourceFile)
{
   File.WriteAllBytes(destinationFile, sourceFile);
}

But i cannot use the string "document" for properties.Resources like this.

('Resources' does not contain a definition for 'document')

How can I make this work?

18
  • This is not how foreach works. Commented Nov 17, 2015 at 9:25
  • 2
    Possible duplicate of Get Resources with string Commented Nov 17, 2015 at 9:26
  • @ASh: Its not a duplicate, a read the other one. The problem in the other one is they want to retrieve the recources name i guess... ? Commented Nov 17, 2015 at 9:27
  • @Soner Gönöl: Ok, why not? (but this isnt the issue) Commented Nov 17, 2015 at 9:28
  • Nomistake, it does appear to be a duplicate, that is exactly what you're trying to do Commented Nov 17, 2015 at 9:31

2 Answers 2

1

You can get the resource by name like this:

string[] documents = new string[] { "one", "two", "three"};
foreach (var document in documents)
{
    var unmanagedMemoryStream = Resources.ResourceManager.GetStream(document);

    var memoryStream = new MemoryStream();
    unmanagedMemoryStream.CopyTo(memoryStream);
    memoryStream.Position = 0;
    byte[] bytes = memoryStream.ToArray();

    extractFile(String.Format(@"C:\temp\{0}.doc", document),
                bytes);
}

There are several methods available on ResourceManager that might be better suited depending on the type of the resource: GetStream, GetString or GetObject.

Sign up to request clarification or add additional context in comments.

2 Comments

i get the error that properties recources doesnt contain definition for getstream...
Cannot convert from 'System.IO.UnmanagedMemoryStream' to 'byte[]'
0

As a solution to the problem i zipped all needed files and added this 1 file to the resources. I extracted it to temp folder and unzipped it to the appropriate locations as needed...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.