title: c# How to access opened Excel file?
body: <p>please help me with this stuff!</p>
  
  <p>In my new project I need to access some Excel files. The names of all of them are known. It's easy to open this files and write data in. If excel objects are stored to the list, then it's easy to re-access already opened files while program still running. Please, take a look to the example below.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using Excel = Microsoft.Office.Interop.Excel;
  
  namespace ExcelTutorial
  {
      public partial class Form1 : Form
      {
          List&lt;TextBox&gt; textToExcel = new List&lt;TextBox&gt;();
          List&lt;string&gt; s_excelFiles = new List&lt;string&gt; { "Workbook1.xlsx", "Workbook2.xlsx", "Workbook3.xlsx", "Workbook4.xlsx", "Workbook5.xlsx" };
          List&lt;Excel.Application&gt; l_excelApplication = new List&lt;Excel.Application&gt;();
          List&lt;Excel.Workbook&gt; l_excelWorkbook = new List&lt;Excel.Workbook&gt;();
          List&lt;Excel.Worksheet&gt; l_excelWorksheet = new List&lt;Excel.Worksheet&gt;();
          bool fileOpened = false;
  
          public Form1()
          {
              InitializeComponent();
              textToExcel.Add(textBox1); textToExcel.Add(textBox2); textToExcel.Add(textBox3);
              textToExcel.Add(textBox4); textToExcel.Add(textBox5);
  
          }
  
          private void btn_Unopened_Click(object sender, EventArgs e)
          {
  
              string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
  
              if (fileOpened != true)
              {
  
                  foreach (var item in s_excelFiles)
                  {
                      Excel.Application newExcelApp = new Excel.Application();
                      Excel.Workbook workbook = newExcelApp.Workbooks.Open(path + "\\" + item
                                                   , ReadOnly: false, Editable: true);
                      Excel.Worksheet worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                      newExcelApp.Visible = true;
                      ((Excel.Range)worksheet.Cells[1, 1]).Value = textToExcel[s_excelFiles.IndexOf(item)].Text;
                      l_excelApplication.Add(newExcelApp);
                      l_excelWorkbook.Add(workbook);
                      l_excelWorksheet.Add(worksheet);      
                  }
                  fileOpened = true;
              }
              else
              {
                  foreach (var item in l_excelWorksheet)
                  {
                      ((Excel.Range)item.Cells[1, 1]).Value = textToExcel[l_excelWorksheet.IndexOf(item)].Text;
  
                  }
              }
          }
  
          private void btn_Opened_Click(object sender, EventArgs e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>However, there is possibility that user has workbook already opened at the moment when program runs. Obviously, Excel sends the message to the user asking for workbook reopening. This could cause data losing. 
  Of course I can check if file is opened before accessing it how it was described <a href="https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use">here</a> with following code:</p>
  
  <pre><code>try
  {
      stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  }
  </code></pre>
  
  <p>And then just to ask user to close that file. But I believe there is another way to do it better.</p>
  
  <p>So, my question is: HOW to access file which was already opened by user?</p>
  
  <p>The ExcelTutorial solution is available <a href="https://drive.google.com/file/d/0BztZnQZx6WFQclowMDI2d1Z5Um8/view?usp=sharing" rel="nofollow noreferrer">here</a>!</p>