title: Connecting to MS access database
body: <p>I recently started learning C#  WPF (using MS VS 2013 express) and I´ve tried to connect to my access database without any success, the problem I have is that whenever I try to make a connection I get this exception <strong>&quot;Not a valid file name&quot;</strong>.</p>
  <p>I realized later than I should have (and after a ton of googling on the matter) that it must have had something to do with my connection string that follows :</p>
  <pre><code>connect.ConnectionString = @&quot;Provider=Microsoft.ACE.OLEDB.12.0;
  Data Source=‪D:\\Google Drive\\Programmering\\C#\\WpfApplication3\\WpfApplication3\\bin\\Debug\\sensors\\MPU6050.accdb; 
  Persist Security Info=False;&quot;;
  </code></pre>
  <p>And this path is copied from the properties/security tab of the file so that should be correct.
  I have also tried with</p>
  <pre><code>connect.ConnectionString =&quot;Provider=Microsoft.ACE.OLEDB.12.0;
  Data Source=‪D:\Google Drive\Programmering\C#\WpfApplication3\WpfApplication3\bin\Debug\sensors\MPU6050.accdb; 
  Persist Security Info=False;&quot;;
  </code></pre>
  <p>Which is the same but excluding the <strong>@</strong> in the beginning.</p>
  <p>I have tried to debug it as Any CPU, x64 and x86 and the only difference is that the 2 latter options returned the following exception as soon as I ran the application before even manually triggering the event that tried to connect to my database.</p>
  <blockquote>
  <p>An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code</p>
  <p>Additional information: Could not find a part of the path 'D:\Google Drive\Programmering\C#\WpfApplication3\WpfApplication3\bin\x64\Debug\sensors'.</p>
  </blockquote>
  <p>I assumed that this exception var pretty much the same as when I debugged it as Any CPU.</p>
  <p>Here is my C# code <code>MainWindow.xaml.cs</code> and my XAML code isn´t relevant as I have everything regarding the database in my code behind. (as far as I know)
  It will probably seem like a joke to you but as I said earlier I'm just starting out with WPF (and C#) and I myself already know that I could of handled things more effective in some ways.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.IO.Ports;
  using System.IO;
  using System.Data.OleDb;
  using System.Data;
  using System.Windows.Threading;
  
  namespace WpfApplication3
  {
      public partial class MainWindow
      {
          public MainWindow()
          {
              InitializeComponent();
              avbComPort.Text = &quot;COM Port&quot;;
              addSensors();            
             
              foreach (string s in SerialPort.GetPortNames()) 
              {
                  ComboBoxItem cbi = new ComboBoxItem();
                  cbi.Content = s;
                  avbComPort.Items.Add(cbi);
              }
          }
  
          public void addSensors()
          {
              string dynamicPath = System.IO.Directory.GetCurrentDirectory();
              string fullPath = dynamicPath + &quot;\\sensors&quot;;
              string[] sensors = Directory.GetFiles(fullPath);
              int fileQuantity = sensors.Length -1;
  
              for (int i = 0; i &lt;= fileQuantity ; i++)
              {
                  string path = sensors[i];
                  string[] pathArr = path.Split('\\');
                  string[] fileArr = pathArr.Last().Split('.');
                  string fileName = fileArr.First().ToString();
                  MenuItem sensor = new MenuItem {Header = fileName};
                  sensor.Click += new RoutedEventHandler(sensor_Click);
                  confSensors.Items.Add(sensor);
  
              }
          }
  
          public void sensor_Click(Object sender, RoutedEventArgs e)
          {
              MenuItem sensor = sender as MenuItem;
              TabItem tab = new TabItem { Header = sensor.Header, Width = sensorTab.Width, Height = sensorTab.Height };
              DataGrid dataLog = new DataGrid() { Name = &quot;dataLog&quot;, IsReadOnly = true, Width = 300, Height = 500, HorizontalAlignment = 0, VerticalAlignment = 0, AutoGenerateColumns = true, ItemsSource = &quot;Binding&quot;};
  
              string filePath = System.IO.Directory.GetCurrentDirectory() + &quot;\\sensors&quot; + sensor.Header + &quot;.accdb&quot;;
              
              try //code regarding the database connection
              {
                  OleDbConnection connect = new OleDbConnection();
                  connect.ConnectionString = &quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪D:\\Google Drive\\Programmering\\C#\\WpfApplication3\\WpfApplication3\\bin\\Debug\\sensors\\MPU6050.accdb; Persist Security Info=False;&quot;;
                  connect.Open();
                  dbStatusLbl.Content = &quot;Connection to database established successfully&quot;;
                  connect.Close();
              }
              catch(Exception ex)
              {
                  MessageBox.Show(&quot;A problem occured while trying to establish a stable connection to the database:  &quot; + ex.Message, &quot;A wild error has appeared&quot;, MessageBoxButton.OK, MessageBoxImage.Error);
              } // end of that code
              Grid grid = new Grid() { Height = tab.Height, Width = tab.Width};
              grid.Children.Add(dataLog);
              tab.Content = grid;
              sensorTab.Items.Add(tab);
          }
  
          private void sensorTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
          }
      }
  }
  </code></pre>
  <p>I have also tried to obtain the file name programmatically with these snippets</p>
  <pre><code>string filePath = System.IO.Directory.GetCurrentDirectory() + &quot;\\sensors&quot; + sensor.Header + &quot;.accdb&quot;;
  
  connect.ConnectionString = &quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|filePath|‪; Persist Security Info=False;&quot;;
  </code></pre>
  <p>Where <code>\\sensors</code> is the folder inside my bin\debug folder and <code>sensor.Header</code> comes from the <code>public void addSensors()</code> method and that has worked explicitly in a previous context where I stored information in .txt files instead of MS access db, as you see this would result in the same string as the full-path and as expected, it returns the exact same exception.</p>
  <p>Furthermore I have Office 2013 64 bit, Windows 10, and Visual Studio 2013 express for desktop, all drivers updated.</p>
  <p>I have tried everything I can imagine and searched the web for hours now and this is my last resort, if you could help even by just sending a link to a possibly helping webpage I would be grateful.
  Thanks in advance and sorry for my English as it´s not my first language.</p>
  <p><strong>EDIT</strong>  I did not have MS Access open while trying to connect with it as I understand that it uses some sort of protection whilst being open?</p>