Click here to Skip to main content
13,005,991 members (106,327 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi Everyone...
I want to save a file in user's Desktop

my code is
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
File.WriteAllText(path, This is created by Animesh Datta.....);

but here exception is occurred . Access to the path is denied.

how can i save a file in desktop ?
Posted 14-Sep-12 2:33am
Updated 14-Sep-12 9:11am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your "path" variable contains the folder name, not a filename. You can append your filename and write the file with a StreamWriter, e.g.
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Testfile.txt");
using (StreamWriter writer = new StreamWriter(filename))
    writer.Write("This is created by Animesh Datta.....");

Also note that the Desktop can be read-only due to the security settings of your customer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

hi dear

try this

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filepath = path + "\\myfile.txt";
krushna chandra jena 14-Sep-12 9:11am
Thank You...
Sergey Alexandrovich Kryukov 18-Feb-13 13:54pm
Correct this time, a 5.
Again, please be careful with answers, only answer when you know things deeply.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


Try the above code given by Bernhard Hiller if it does't work then your current user account does't has permission to write file in operating system folder try with administrator account.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100