Text file writing in c#

May 28 2018 5:14 AM
I want to write a text file in c#... but it is giving error
 
Access to the path 'C:\Program Files(x86)\Anil\demoSettings.txt' is denied.
  1. string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);  
  2. string FileName = Path.Combine(executableLocation, "demoSettings.txt");  
  3. var permissionSet = new PermissionSet(PermissionState.None);  
  4. var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, FileName);  
  5. permissionSet.AddPermission(writePermission);  
  6. if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))  
  7. {  
  8. using (FileStream fstream = new FileStream(FileName, FileMode.Create))  
  9. using (TextWriter sw = new StreamWriter(fstream))  
  10. {  
  11. sw.WriteLine("Name," + "Anil");  
  12. sw.WriteLine("Code," + "C#");  
  13. }  
  14. }  
how to fix this ??? can anyone help me out???

Answers (6)