0

I seem to be having a problem opening an excel file with a relative path in visual studio. The file is being saved to:

C:\Users\UserName\Documents\Filename.xls

on any machine. I have tried using:

Process.Start(@"~\Documents\Filename.xls");

to open the excel file, but it throws an error saying the file location cannot be found. Is there something I am doing wrong? Thank you in advance.

3 Answers 3

3

The '~' character is used in Linux.

With the following code you can obtain the path for special folders:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 

The MyDocuments can be replaced for other Windows specific folder like:

  • UserProfile
  • MyPictures
  • Desktop
Sign up to request clarification or add additional context in comments.

Comments

0

You could use combine to get the full path

var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fullPath.Combine(folderPath, "filename.xls");
Process.Start(fullPath);

If you know the relative path to the working folder use

var fullPath = Path.GetFullPath(relPath);

Comments

0

'~' is not a valid path identifier in Windows (It does refer to the home directory on *nix). I am not sure what you are trying to achieve, but perhaps you want SpecialFolder see description here at MSDN?

var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Will return: C:\Users\'UserName'\Documents\

Comments

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.