1

My current project uses a direct file path of this excel document to read information off of the excel file. I need to get my project ready for release so I cannot have the project hard code a file path in the form of a string.

I want to embed the Excel File in my resource, which I have done, but know how can I get the file path from Resource, and send a file path to the class which reads the Excel file. The class must be feed a filepath so I was thinking of making a copy of this Excel file, and in the Temp folder then referenceing the file path for the class to read the Excel file.

  FileName = @"D:\SomeFolder\ExcelFile.xlsx"; //This is the old code, hard coded

//I need code  that is going to make a copy of this file from the Resources and save it somewhere in a temp folder, but then give me
  the File path in the form of a string.

            string FileName; 
         // I need the file name to have the directory of this excel that is in the Resource folder



            //Call Class to Create XML File and store Data from BIN File Locally on Program

            ReadExcel_CreateXML = new ExcelRecorder(FileName);
tennis779
  • 338
  • 2
  • 6
  • 19

2 Answers2

0

I'm not sure if this the best solution, but it will work:

1st get the byte[] array of the file in your resources:

byte[] fileByteArray = global::YourProjectNameSpace.Properties.Resources.ExcelFileName

2nd Export the file to a temporary location using this function: (I got from here: Write bytes to file)

public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
        {
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream =
                   new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                            System.IO.FileAccess.Write);
                // Writes a block of bytes to this stream using data from
                // a byte array.
                _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

                // close file stream
                _FileStream.Close();

                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}",
                                  _Exception.ToString());
            }

            // error occured, return false
            return false;
        }

And last access that temporary file like you normally would

Use:

Just create a button in a form and put this code in the button's click event

private void button1_Click(object sender, EventArgs e)
    {
        byte[] fileByteArray = global::YourProjectNameSpace.Properties.Resources.ExcelFileName;

        if (ByteArrayToFile(@"C:\Temp\file.xlsx", fileByteArray))
        {
            //File was saved properly
        }
        else
        {
            //There was an error saving the file
        }
    }

Hope it works

Community
  • 1
  • 1
Manuvo
  • 748
  • 5
  • 15
0

Something else to think about is that you are probably reading the current files using a FileStream and either a BinaryReader or StreamReader. If that's the case, the consumer of the file could be written to accept an arbitrary Stream instead and then you can create a MemoryStream to pass to the consuming class:

// The resource will be a byte array, I'm just creating a
// byte array manually for example purposes.
var fileData = System.Text.Encoding.UTF8.GetBytes("Hello\nWorld!");

using (var memoryStream = new MemoryStream(fileData))
using (var streamReader = new StreamReader(memoryStream))
{
    // Do whatever you need with the file's contents
    Console.WriteLine(streamReader.ReadLine());
    Console.WriteLine(streamReader.ReadLine());
}

This approach means you won't be cluttering up the client computer with temporary files that you'll need to clean up. It also means your consuming class will become more flexible if you ever need to process data over any other type of Stream.

Anthony
  • 9,451
  • 9
  • 45
  • 72