title: Open a CSV file in Excel via C#?
body: <p>Using a solution presented by <a href="https://stackoverflow.com/questions/1008556/export-listview-to-csv">Max Galkin</a>, I put together the following procedure which transfers all data in a ListView to a .CSV file. I'm having issues with 2 parts however:</p>
  
  <ol>
  <li>I have not figured out how to successfully open the newly created .CSV when user clicks <code>YES</code> on the appropriate dialogue.</li>
  <li>It seems that some users (on Virtual PC's unlike myself) are not able to open or view the the new file unless they first close down the application. I believe this is due to the Application still having a process tied to the created file. The message that appears when trying to open the file is: <code>'FileName.csv' cannot be accessed. The file may be corrupted, located on a server that is not responding, or read-only.</code></li>
  </ol>
  
  <p>Here is my current code:</p>
  
  <pre><code>private void btnCSVExcel_Click(object sender, EventArgs e)
          {
              if (!Directory.Exists(@"C:\TEMP\"))
              {
                  Directory.CreateDirectory(@"C:\temp\");
              }
              if (!Directory.Exists(@"C:\temp\Exported CSV Files\"))
              {
                  Directory.CreateDirectory(@"C:\temp\Exported CSV Files\");
              }
  
              string csvPath = @"C:\temp\Exported CSV Files\";
  
              ListViewToCSV(lvData, csvPath, false);
          }
  
          // https://stackoverflow.com/questions/1008556/export-listview-to-csv
          public static void ListViewToCSV(ListView listView, string filePath, bool includeHidden)
          {
              string csvFileName = filePath + DateTime.Now.ToString("yyyy-MM-dd-hh.mm.ss.ffffff") + ".csv";
  
              //make header string
              StringBuilder result = new StringBuilder();
              WriteCSVRow(result, listView.Columns.Count, i =&gt; includeHidden || listView.Columns[i].Width &gt; 0, i =&gt; listView.Columns[i].Text);
  
              //export data rows
              foreach (ListViewItem listItem in listView.Items)
              {
                  WriteCSVRow(result, listView.Columns.Count, i =&gt; includeHidden || listView.Columns[i].Width &gt; 0, i =&gt; listItem.SubItems[i].Text);
              }
  
              File.WriteAllText(csvFileName, result.ToString());
  
              var openCSVFile = MessageBox.Show("Export Complete. CSV file saved as: " + csvFileName + ". \n\n Open File Now?", "CSV Exported", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
              if (openCSVFile == DialogResult.Yes)
              {
                  // NEED TO OPEN THE CSV FILE IN EXCEL....?
                  File.Open(csvFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
              }
          }
  
          private static void WriteCSVRow(StringBuilder result, int itemsCount, Func&lt;int, bool&gt; isColumnNeeded, Func&lt;int, string&gt; columnValue)
          {
              bool isFirstTime = true;
              for (int i = 0; i &lt; itemsCount; i++)
              {
                  if (!isColumnNeeded(i))
                      continue;
  
                  if (!isFirstTime)
                      result.Append(",");
                  isFirstTime = false;
  
                  result.Append(String.Format("\"{0}\"", columnValue(i)));
              }
              result.AppendLine();
          }
  </code></pre>
  
  <p>Anyone have any thoughts for how I might resolve these last 2 issues? I thought <code>File.Open()</code> may suffice for the first issue, but visually nothing occurs and the document does not open.</p>