illigal characters in path

  • 338 Views
  • Last Post 22 January 2021
  • Topic Is Solved
cabalsdemon posted this 20 January 2021

in your youtube video on excel sheets in a datagridview

private void button3_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel Workbook |*.xlsx|Excel 97-2000 Workbook|*.xls" })
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //txtFilename.Text = openFileDialog.FileName;
                    using (var stream = File.Open(openFileDialog.Filter, FileMode.Open, FileAccess.Read))
                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                            });
                            tableCollection = result.Tables;
                            CboSheet.Items.Clear();
                            foreach (DataTable table in tableCollection)

                                CboSheet.Items.Add(table.TableName);

                            DataTable dt = tableCollection[CboSheet.SelectedItem.ToString()];
                            DataExcel.DataSource = dt;
                        }
                    }
                }

            }
        }
using (var stream = File.Open(openFileDialog.Filter, FileMode.Open, FileAccess.Read))

 

this is giving me a   System.ArgumentException: 'Illegal characters in path.'

System.ArgumentException
  HResult=0x80070057
  Message=Illegal characters in path.
  Source=mscorlib
  StackTrace:
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.GetFileName(String path)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.Open(String path, FileMode mode, FileAccess access)
   at excelread2.Form1.button3_Click(Object sender, EventArgs e) in C:\Users\ezewh\source\repos\excelread2\excelread2\Form1.cs:line 31
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at excelread2.Program.Main() in C:\Users\ezewh\source\repos\excelread2\excelread2\Program.cs:line 19

  This exception was originally thrown at this call stack:
    [External Code]
    excelread2.Form1.button3_Click(object, System.EventArgs) in Form1.cs
    [External Code]
    excelread2.Program.Main() in Program.cs

please help thank you

Order By: Standard | Newest | Votes
cabalsdemon posted this 21 January 2021

i will answer my question for anyone having this problem

i had to take the filter off of 

openFileDialog.Filter

so this is what my code looks like now

 

OpenFileDialog openFileDialog = new OpenFileDialog();
        DataTableCollection tableCollection;
        private void button3_Click(object sender, EventArgs e)
        {
            //using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "*.xlsx |*.xls" })
            //{
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilename.Text = openFileDialog.FileName;
                   // using (var stream = File.Open(openFileDialog.Filter, FileMode.Open, FileAccess.Read))
                    using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))

                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                            });
                            tableCollection = result.Tables;
                            CboSheet.Items.Clear();
                            foreach (DataTable table in tableCollection)

                                CboSheet.Items.Add(table.TableName);

                            //DataTable dt = tableCollection[CboSheet.SelectedItem.ToString()];
                            //DataExcel.DataSource = dt;
                        }
                    }
                }

            //}
        }

the filter was messing up the address to the file

admin posted this 22 January 2021

You should modify your code

using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read)){
}

Just change openFileDialog.Filter to openFileDialog.FileName

Close