# Thread: opening an exel file in a c# program

1. ## opening an exel file in a c# program

Hi

I am getting the following error while opening the exel file. I am sure the path and the file name is correct.
Code:
```'G:\myCodeProgram\swiming\beforeMatch\Files\Input\arrange.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.

If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.

System.Runtime.InteropServices.COMException was unhandled
Message="'G:\\myCodeProgram\\swiming\\beforeMatch\\Files\\Input\arrange.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted."
Source="Microsoft Office Excel"
ErrorCode=-2146827284
StackTrace:
at matchProvider.mainInfo.button1_Click(Object sender, EventArgs e) in G:\myCodeProgram\swiming\beforeMatch\1\matchProvider\matchProvider\mainInfo.cs:line 116
at System.Windows.Forms.Control.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.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(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at matchProvider.Program.Main() in G:\myCodeProgram\swiming\beforeMatch\1\matchProvider\matchProvider\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
InnerException:```
I use visual Studio 2008 , win7 (86X)

and here is the code
Code:
```object opt = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.ApplicationClass();
Excel.Sheets Sheets;
Excel.Workbook wArrange;

wArrange = excel.Workbooks.Open(path+"\arrange.xlsx", opt, false, opt, opt, opt, true, opt, opt, opt, opt, opt, opt, opt, opt);
Sheets = wArrange.Worksheets;
teamNum = wArrange.Sheets.Count;

wArrange.Close(true, opt, opt);```

Can you plz tell me what is wrong?

Tnx
arian

2. Code:
`path + "\\arrange.xlsx"`
EDIT: Look at the original error message more closely. It has \\ except for the last one which has single.

3. What a big/stupid mistake I have done!

Thanks a lot for your help

4. What a big/stupid mistake I have done!
Nah. Just an oversight. It happens to all of us.

5. You can also use the @"path1\path2" syntax, which escapes characters within the string automatically. Or use Path.Combine()

6. Originally Posted by indigo0086
Or use Path.Combine()
QFT. I got tired of making the same error as the OP; I always use Path.Combine() now. The @"" syntax is fine if you have a hardcoded path, but if you've got to piece parts together, Path.Combine() is the way to go.