Click here to Skip to main content
12,450,347 members (42,330 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
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
v2
Rate this: bad
 
good
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.
  Permalink  
Rate this: bad
 
good
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";
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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
Web01 | 2.8.160826.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2016
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