Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. using (File.Create("somefile"))
  2. {
  3. //nothing here...
  4. };
  5.  
  6. string path = @"c:tempMyTest.txt";
  7. try
  8. {
  9.  
  10. // Delete the file if it exists.
  11. if (File.Exists(path))
  12. {
  13. // Note that no lock is put on the
  14. // file and the possibility exists
  15. // that another process could do
  16. // something with it between
  17. // the calls to Exists and Delete.
  18. File.Delete(path);
  19.  
  20. //Here you can create a new file with different name also using while loop
  21. }
  22.  
  23. // Create the file.
  24. using (FileStream fs = File.Create(path))
  25. {
  26. Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
  27. // Add some information to the file.
  28. fs.Write(info, 0, info.Length);
  29. }
  30.  
  31. // Open the stream and read it back.
  32. using (StreamReader sr = File.OpenText(path))
  33. {
  34. string s = "";
  35. while ((s = sr.ReadLine()) != null)
  36. {
  37. Console.WriteLine(s);
  38. }
  39. }
  40. }
  41.  
  42. catch (Exception Ex)
  43. {
  44. Console.WriteLine(Ex.ToString());
  45. }
Add Comment
Please, Sign In to add comment