Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi Everyone...
I want to save a file in user's Desktop

my code is
C#
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
Updated 7-Mar-21 0:34am
v2

Your "path" variable contains the folder name, not a filename. You can append your filename and write the file with a StreamWriter, e.g.
C#
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.
 
Share this answer
 
hi dear

try this

C#
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filepath = path + "\\myfile.txt";
 
Share this answer
 
v2
Comments
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.
—SA
Hi,

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900