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?

Nomistake
  • 893
  • 2
  • 17
  • 32
  • This is not how `foreach` works. – Soner Gönül Nov 17 '15 at 09:25
  • 2
    Possible duplicate of [Get Resources with string](http://stackoverflow.com/questions/17828774/get-resources-with-string) – ASh Nov 17 '15 at 09: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... ? – Nomistake Nov 17 '15 at 09:27
  • @Soner Gönöl: Ok, why not? (but this isnt the issue) – Nomistake Nov 17 '15 at 09:28
  • Nomistake, it does appear to be a duplicate, that is exactly what you're trying to do – Sayse Nov 17 '15 at 09:31
  • @Sayse: I have read it before, but it didnt help me. But i'm willing to read it again. – Nomistake Nov 17 '15 at 09:34
  • The op in that question wasn't trying to get the name of the resource but their resource was of type `String`. Perhaps your's isn't. If so, there are other [methods in ResourceManager](https://msdn.microsoft.com/en-us/library/system.resources.resourcemanager%28v=vs.110%29.aspx) that will work – Sayse Nov 17 '15 at 09:36
  • Maybe it wasnt so clear but my resources are byte[] ... and they have a name. I want to call them by the name bij passing the string to Properties.Resources... – Nomistake Nov 17 '15 at 09:40
  • It looks like you expect `properties.Resources.document` to be 'translated' to `properties.Resources.one`, `properties.Resources.two`, and so on. That's not how it works in C#. You could use reflection for this, but `ResourceManager` already provides methods that accept a string and return the object or stream you're looking for, so just use those, as Sayse already mentioned. – Pieter Witvoet Nov 17 '15 at 09:40
  • @PieterWitvoet Yes, you got it. I know this isnt working like this in c#, therefore this question... – Nomistake Nov 17 '15 at 09:42
  • I'll have a look at the ResourceManager... But its a new one for me – Nomistake Nov 17 '15 at 09:42
  • it may be of further help if you can show how properties.Resources.document is being used in `extractFile` method. Perhaps you just need to pass the resource class/file name (document here) and instantiate `ResourceManager` accordingly as others mentioned. – Nikhil Vartak Nov 17 '15 at 09:44
  • Well, `properties.Resources` is of type `ResourceManager`, right? So just call `properties.Resources.GetStream(document)` - or `GetString` or `GetObject`, depending on what kind of resource you're working with. – Pieter Witvoet Nov 17 '15 at 09:46
  • @PieterWitvoet Thank you for your time. But it isnt working. Also the so called duplicate isnt helping. The resources are plain word documents and they are of type byte[] when i look in the recources of the solution. Maybe my question isnt clear, but i just want to use a variable in Properties.Resources.StringVariable where StringVariable is a variable that i can assing a value before execution... – Nomistake Nov 17 '15 at 11:06
  • What do you mean with 'it isn't working'? – Pieter Witvoet Nov 17 '15 at 11:18
  • @PieterWitvoet I added the methode extractFile. I forgot to add it. Mybe it helps to have a clear view on the problem... – Nomistake Nov 17 '15 at 11:24
  • So what exactly isn't working? What errors do you get? We're not psychic here... ;) – Pieter Witvoet Nov 17 '15 at 11:39
  • Just a sec, i'll get back to this. thanks – Nomistake Nov 20 '15 at 15:11

2 Answers2

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.

Kvam
  • 2,148
  • 1
  • 20
  • 32
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...

Nomistake
  • 893
  • 2
  • 17
  • 32