Click here to Skip to main content
Rate this: bad
good
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
Edited 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 at 14-Sep-12 9:11am
   
Thank You...
Sergey Alexandrovich Kryukov at 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
0 OriginalGriff 490
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Vinay Mistry 160
4 Sergey Alexandrovich Kryukov 153
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
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