Advertisement
Guest User

Untitled

a guest
Jan 14th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication5
  8. {
  9. class Program
  10. {
  11.  
  12.  
  13. static void SetAllFilesAsReadOnly(string rootPath)
  14. {
  15. Console.ForegroundColor = ConsoleColor.White;
  16.  
  17. int changed = 0;
  18. List<string> files = new List<string>();
  19.  
  20. foreach (string file in System.IO.Directory.EnumerateFiles(rootPath, "*.*", System.IO.SearchOption.AllDirectories))
  21. {
  22.  
  23.  
  24. System.IO.FileAttributes attr = System.IO.File.GetAttributes(file);
  25.  
  26.  
  27.  
  28. if ((attr & System.IO.FileAttributes.ReadOnly) == 0)
  29. {
  30. attr = attr | System.IO.FileAttributes.ReadOnly;
  31.  
  32. System.IO.File.SetAttributes(file, attr);
  33.  
  34. changed++;
  35. files.Add(file);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46. if (changed > 0)
  47. {
  48. Console.SetCursorPosition(2, 4);
  49. Console.WriteLine("These are the files changed:");
  50. Console.ForegroundColor = ConsoleColor.Green;
  51.  
  52. foreach (string file in files)
  53. {
  54.  
  55. Console.WriteLine(" " + file);
  56.  
  57. }
  58.  
  59. Console.ForegroundColor = ConsoleColor.White;
  60. Console.WriteLine("");
  61. Console.WriteLine(" Write-protection was set on {0} files.", changed);
  62.  
  63. }
  64. else
  65. {
  66. Console.SetCursorPosition(2, 2);
  67. Console.Write("Write-protection was not changed on any files.", changed);
  68. }
  69. Console.ForegroundColor = ConsoleColor.Black;
  70. Console.ReadKey();
  71. }
  72.  
  73. static void Main(string[] args)
  74. {
  75.  
  76. Console.SetCursorPosition(2, 2);
  77. Console.Write("Please enter the path of the directy you wish to check: ");
  78. Console.ForegroundColor = ConsoleColor.Green;
  79. string pathstring = Console.ReadLine();
  80.  
  81.  
  82. Console.Clear();
  83. SetAllFilesAsReadOnly(pathstring);
  84.  
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement