Get path from desktop

Asked

Viewed 3,305 times

2

Boas, last week I put here a question:

Automatically save to Default Folder C#

I have now created another form, and I want Load to automatically fetch the file from the Desktop. However I can not put the code below, because if the program was installed on other machines, the program will give error. How do I put the code to go to the Desktop?

CODE

  System.Diagnostics.Process.Start(@"C:\Users\NOME\Desktop\File.txt");

Thank you!

  • A small note: in the future, avoid including the language (C#) in the question title. That’s why there are tags.

  • I changed the title to better describe the problem.

  • Right friend @dcastro! ;)

1 answer

4


Use the Enum Environment.SpecialFolder.Desktop

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath = Path.Combine(desktop, "File.txt");
  • Amigo @dcastro Load is not opening the document...

  • Debug and check if the value of filePath this' correct.

  • How so @dcastro?

  • @Godfathersantana You said that "Load is not opening the document". It is impossible to diagnose the problem just with this information. What happened? What code is in the Load method? What part of the code failed? Was an exception made? If so, what? What are the values of the local variables? Is the file path correct? The file exists on disk?

  • Buddy, basically I debug, the form opens but the program does not automatically open the FILE.txt that is in the Desktop... The code that is in Load, is what you mentioned above, there is no error.

  • @Godfathersantana You called Process.Start after getting the way?

  • Not @dcastro, add: "System.Diagnostics.Process.Start(@"Desktop FILE.txt");" ?

  • @Godfathersantana Try to read and understand the code I posted. Only making copyThis is not a good habit. The code only builds the path to the file, and stores the path in the variable filePath. Then you need to open the file, with Process.Start(filePath)

  • Friend, I managed to solve, it was a mistake in my code. Thanks!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.