0

I try to show an existing excel file when a user press the link which is in my c# desktop application. I put my excel files into my project folder. nevertheless when I set up my project into different computers, paths of excel files will change. I couldn't find a way unrelated with direct path like C:\example.xlsx while opening excel files. How can I solve this problem? Thanks already..

1
  • can you use with double slash like this "c:\\example.xlsx" or @"c:\example.xlsx" ? Commented Sep 16, 2011 at 11:41

3 Answers 3

1

I would suggest using the System.IO.Path.GetTempFileName() and copy the Excel file to the temporary file.

Or

Use the Application.StartupPath property.

Sign up to request clarification or add additional context in comments.

Comments

0

If these files are stored inside your applicaton folder try to use:

string excelFilePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "excelfiles\\myexcelfile.xlsx");

Otherwise if you have predefined set of the files you need to have some setting where files are stored.

If you just need to allow user to specify where file stored - use OpenFileDialog.

2 Comments

Thanks for solution:) but it gives an error in "Application.ExecutablePath" part. Program doesn't decide the which "Application" . I tried Microsoft.Office.Interop.Excel.Application.ExecutablePath but still gives error. System.Windows.Forms.Application.ExecutablePath also doesn't properly work. what should I do?
try System.Reflection.Assembly.GetExecutingAssembly().Location
0

Two things:

If you don't mind MS GUI implementation, try the OpenFileDialog to let your user (if appropriate to do so) select the correct file:

var openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "xlsx files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;

Edit: Then use openFileDialog1.Filename to get the filename.

OR

Use a relative path name if the file is always in the same relational directory:

StreamReader reader = new StreamReader("..\\'folder'\\'file'");

I find that one of these two ways will always work; dependant on if I want the user to choose the file or not.

Edit: Once you have the file name, use Microsoft.Office.Interop.Excel namespace to gain control of the file in excel, if that is desired.

Hope this helps!

4 Comments

thanks but I couldn't use openfiledialog because user may not have that excel files..
I thought you said it was an existing excel file
yes, but I have, not users. so I want to give excel files to the users in my program setup
But they can pick the excel file from anywhere and the OpenFileDialog can return the path of the file to you. Then you will have know where that file is and what to open. And if you open it using Excel Interop then you can have full control over the Excel object from within C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.