× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to open file that is on desktop

Gayot Fow
Feb 05, 2015
<p>It's likely that your program couldn't find the file because it is looking in the wrong place. You can correct that with something like this...</p> <pre><code> private void ReadFromDesktop(string fileName) { string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fullName = System.IO.Path.Combine(desktopPath, fileName); using (StreamReader steamReader = new StreamReader(fullName)) { string content = steamReader.ReadToEnd(); } } </code></pre> <p>This method takes the name of a file (presumably that which you entered on the console) and then queries the environment for the path to your desktop.</p> <p>Then it uses the Path.Combine method to create a fully qualified name for the file. The result is processed by the stream reader.</p> <p>The point being that if you provide only the file name, the app will look in its root directory (which is, as you wrote, the debug directory). The hint for that was the error message telling you that it created a full path by using the name of your debug directory.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/19624731/How%20to%20open%20file%20that%20is%20on%20desktop/19624889">Stack Overflow</a>.</p>
comments powered by Disqus