Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. // Check if directory exists
  4. if (Directory.Exists(@"C:UsersrsDesktopTest)"))
  5. {
  6. // Do nothing
  7. }
  8.  
  9. else
  10. {
  11. Directory.CreateDirectory(@"C:UsersrsDesktopTest");
  12. }
  13.  
  14. if (File.Exists(@"C:UsersrsDesktopTesttest.txt"))
  15. {
  16. // Do nothing
  17. }
  18.  
  19. else
  20. {
  21. File.Create(@"C:UsersrsDesktopTesttest.txt");
  22. }
  23.  
  24. StreamReader sr = new StreamReader(@"C:UsersrsDesktopTesttest.txt");
  25. while (sr.Peek() >= 0)
  26. {
  27. combox_Name2.Items.Add(sr.ReadLine());
  28. }
  29. sr.Close();
  30. }
  31.  
  32. private void btn_Save_Click(object sender, EventArgs e)
  33. {
  34.  
  35. StreamWriter writer = new StreamWriter(@"C:UsersrsDesktopTesttest.txt", true);
  36.  
  37. try
  38. {
  39. Process.Start(combox_Name2.Text, txt_Name.Text);
  40.  
  41. if (combox_Name2.Items.Contains(txt_Name.Text))
  42. {
  43. // Do nothing
  44. }
  45.  
  46. else
  47. {
  48. writer.WriteLine(combox_Name2.Text);
  49. writer.Flush();
  50. writer.Dispose();
  51. combox_Name2.Items.Add(txt_Name.Text);
  52. }
  53. }
  54.  
  55. catch
  56. {
  57. MessageBox.Show("The file '" + txt_Name.Text + "' could not be located", "File could not be located");
  58. }
  59. }
  60.  
  61. using System;
  62. using System.Collections.Generic;
  63. using System.ComponentModel;
  64. using System.Data;
  65. using System.Drawing;
  66. using System.Linq;
  67. using System.Text;
  68. using System.Windows.Forms;
  69. using System.IO;
  70. using System.Diagnostics;
  71.  
  72. namespace FileHandling
  73. {
  74. public partial class Form1 : Form
  75. {
  76. public Form1()
  77. {
  78. InitializeComponent();
  79. }
  80.  
  81. private void Form1_Load(object sender, EventArgs e)
  82. {
  83. // Check if directory exists
  84. if (Directory.Exists(@"C:UsersrsDesktopTest)"))
  85. {
  86. // Do nothing
  87. }
  88.  
  89. else
  90. {
  91. Directory.CreateDirectory(@"C:UsersrsDesktopTest");
  92. }
  93.  
  94. if (File.Exists(@"C:UsersrsDesktopTesttest.txt"))
  95. {
  96. // Do nothing
  97. }
  98.  
  99. else
  100. {
  101. File.Create(@"C:UsersrsDesktopTesttest.txt");
  102. }
  103.  
  104. using (StreamReader writer = new StreamReader(@"C:UsersrsDesktopTesttest.txt"))
  105. {
  106. while (writer.Peek() >= 0)
  107. {
  108. combox_Name2.Items.Add(writer.ReadLine());
  109. }
  110. writer.Close();
  111. }
  112.  
  113. }
  114.  
  115. private void btn_Save_Click(object sender, EventArgs e)
  116. {
  117. using (StreamWriter writer = new StreamWriter(@"C:UsersrsDesktopTesttest.txt", true))
  118. {
  119. try
  120. {
  121. if (combox_Name2.Items.Contains(txt_Name.Text))
  122. {
  123. MessageBox.Show("The task '" + txt_Name.Text + "' already exist in list", "Task already exists");
  124. }
  125.  
  126. else
  127. {
  128. combox_Name2.Items.Add(txt_Name.Text);
  129. writer.WriteLine(txt_Name.Text);
  130. writer.Flush();
  131. writer.Dispose();
  132. }
  133. }
  134.  
  135. catch
  136. {
  137. MessageBox.Show("The file '" + txt_Name.Text + "' could not be located", "File could not be located");
  138. }
  139.  
  140. }
  141. }
  142. }
  143. }
  144.  
  145. using(IDisposable object goes here)
  146. {
  147. // Do something.
  148. }
  149.  
  150. using(StreamWriter writer = new StreamWriter(@"C:UsersrsDesktopTesttest.txt", true))
  151. {
  152. // do your thing here.
  153.  
  154. }
  155.  
  156. StreamWriter writer = new StreamWriter(@"C:UsersrsDesktopTesttest.txt", true);
  157. try
  158. {
  159. // do your thing here.
  160.  
  161. }
  162. finally
  163. {
  164. writer.Dispose();
  165. }
  166.  
  167. combox_Name2.Items.Clear();
  168. if (File.Exists(@"C:UsersrsDesktopTesttest.txt"))
  169. {
  170. using(StreamReader sr = new StreamReader(@"C:UsersrsDesktopTesttest.txt"))
  171. {
  172. while (sr.Peek() >= 0)
  173. {
  174. combox_Name2.Items.Add(sr.ReadLine());
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement