2

I am uploading a file to Api and trying to read the resource key and values. I am getting below exception when I try to read the file.

System.ArgumentException: Stream is not a valid resource file.
at System.Resources.ResourceReader._ReadResources()
at System.Resources.ResourceReader.ReadResources()
at System.Resources.ResourceReader..ctor(String fileName)

Below is the code which I tried.

        [HttpPost]
    public async Task<ActionResult> Post(IFormFileCollection files)
    {
        try
        {
            files = this.Request.Form.Files;
            var tempFolder = Path.GetTempPath();

            foreach (var formFile in files)
            {
                string fileName = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
                string filePath = Path.Combine(tempFolder, fileName);

                if (formFile.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream).ConfigureAwait(false);
                    }

                    var resReader = new ResourceReader(@filePath); // Throwing an exception.
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }

        return this.Ok("Success.");
    }

Below is the request Uri and file.

enter image description here

Am I missing any configuration?

Balanjaneyulu K
  • 3,660
  • 5
  • 24
  • 45
  • `ResourceReader` is meant for `.resources` files - It looks like you might want [`ResXResourceReader`](https://learn.microsoft.com/en-us/dotnet/api/system.resources.resxresourcereader?view=netframework-4.8) for `.resx` files. – Kirk Larkin May 16 '19 at 09:28
  • The class is available in System.Windows.Forms and it is not available in .NET Core. Do we have any alternative for that class? – Balanjaneyulu K May 16 '19 at 09:36
  • I don't know of any alternative. It seems it will be available in .NET Core 3 but that's not ready yet. – Kirk Larkin May 16 '19 at 09:47

2 Answers2

0

ResourceReader reads binary resource files (.resources) not text resource flies (.resx). At compile time the text resource files are compiled to their binary equivalent.

Refer to this link https://learn.microsoft.com/en-us/dotnet/core/extensions/work-with-resx-files-programmatically; on how to work with .resx files programmatically.

If you have the file physically and the definition of the resource in your .resx file, just rebuild your solution and the problem will be solved.

0

Looks like in the .NET 4.8 Framework the ResourceReader reads .resource files and the ResXResourceReader reads .resx files now.

This was different in previous versions of .Net where you could just use the ResourceReader.

To use the ResxResourceReader, add a reference to System.Windows.Forms to your project, and change the code to use the ResXResourceReader instead of the ResourceReader.

Source: https://learn.microsoft.com/en-us/dotnet/api/system.resources.resxresourcereader?view=netframework-4.8

AdamantineWolverine
  • 2,131
  • 1
  • 17
  • 14