【问题标题】:Going from a File Path to using a Resource从文件路径到使用资源
【发布时间】:2014-10-15 11:23:43
【问题描述】:

我当前的项目使用此 excel 文档的直接文件路径从 excel 文件中读取信息。我需要让我的项目准备好发布,所以我不能让项目硬编码字符串形式的文件路径。

我想将 Excel 文件嵌入到我的资源中,我已经完成了,但知道如何从 Resource 获取文件路径,并将文件路径发送到读取 Excel 文件的类。该类必须提供文件路径,因此我正在考虑制作此 Excel 文件的副本,然后在 Temp 文件夹中引用该类的文件路径以读取 Excel 文件。

  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);

【问题讨论】:

    标签: c# visual-studio-2010 resources


    【解决方案1】:

    另外需要考虑的是,您可能正在使用FileStreamBinaryReaderStreamReader 读取当前文件。如果是这种情况,可以将文件的使用者写入接受任意Stream,然后您可以创建MemoryStream 以传递给消费类:

    // 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());
    }
    

    这种方法意味着您不会将需要清理的临时文件弄乱客户端计算机。这也意味着如果您需要通过任何其他类型的Stream 处理数据,您的消费类将变得更加灵活。

    【讨论】:

      【解决方案2】:

      我不确定这是否是最好的解决方案,但它会起作用:

      首先获取资源中文件的 byte[] 数组:

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

      2nd 使用此函数将文件导出到临时位置: (我从这里得到: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;
              }
      

      最后像平常一样访问该临时文件

      用途:

      只需在表单中创建一个按钮,并将此代码放入按钮的点击事件中

      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
              }
          }
      

      希望有效果

      【讨论】:

      • 感谢您的回复,但您所说的 _FileName 是什么意思,这是我的文件名字符串吗?
      • 是的,它是文件名的字符串,我只是添加了一个示例,如果您还有其他问题,请告诉我
      • 我怎么知道它写入文件的位置。
      • 它可以在任何你想要的地方,我使用“C:\Temp\file.xlsx”,但你可以将它更改为你选择的位置
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2017-10-08
      相关资源
      最近更新 更多