Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows.Forms;
  7.  
  8. namespace WindowsFormsApplication2
  9. {
  10. public class File
  11. {
  12. FileStream fs = null;
  13.  
  14. private StreamReader sr = null;
  15. private StreamWriter sw = null;
  16.  
  17. private int recsize;
  18. private long filesize;
  19. private char mode;
  20. private string filepath;
  21.  
  22. public File()
  23. {
  24. }
  25.  
  26. public long RecNum
  27. {
  28. get
  29. {
  30. return filesize / recsize;
  31. }
  32. }
  33.  
  34. private bool checkfile(string _filepath)
  35. {
  36. int fileindex = _filepath.LastIndexOf("\\", _filepath.Length);
  37. string file = _filepath.Substring(fileindex + 1);
  38.  
  39. string directorypath = _filepath.Substring(0, fileindex + 1);
  40. filepath = _filepath;
  41.  
  42. if (Directory.Exists(directorypath))
  43. {
  44. string[] filenames = Directory.GetFiles(directorypath);
  45. foreach (string s in filenames)
  46. if (s.Substring(fileindex + 1) == file) return true;
  47. MessageBox.Show("New file is created");
  48. return true;
  49. }
  50. else
  51. {
  52. MessageBox.Show("Directory doesn't exist");
  53. return false;
  54. }
  55. }
  56.  
  57. public bool open(string _filepath, char _mode, int _recsize)
  58. {
  59. mode = _mode;
  60. recsize = _recsize;
  61.  
  62. if (checkfile(_filepath))
  63. {
  64. switch (mode)
  65. {
  66. case 'C':
  67. fs = new FileStream(_filepath, FileMode.Create, FileAccess.ReadWrite);
  68. break;
  69. case 'R':
  70. fs = new FileStream(_filepath, FileMode.OpenOrCreate, FileAccess.Read);
  71. break;
  72. case 'W':
  73. fs = new FileStream(_filepath, FileMode.OpenOrCreate, FileAccess.Write);
  74. break;
  75. default:
  76. MessageBox.Show("Error: File Access Mode(W or R)");
  77. return false;
  78. }
  79. filesize = fs.Length;
  80.  
  81. try
  82. {
  83. switch (mode)
  84. {
  85. case 'R':
  86. sr = new StreamReader(fs);
  87. break;
  88. case 'W':
  89. sw = new StreamWriter(fs);
  90. break;
  91. }
  92. }
  93. catch (Exception Ex)
  94. {
  95. MessageBox.Show(Ex.Message);
  96. return false;
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102.  
  103. public string read(int _recordnum)
  104. {
  105. if (mode == 'R')
  106. {
  107. if ((filesize / recsize) >= _recordnum)
  108. {
  109. int startP = _recordnum * recsize;
  110. fs.Position = startP;
  111. return sr.ReadLine();
  112. }
  113. MessageBox.Show("Error:Record index out of bound!");
  114.  
  115. }
  116. else MessageBox.Show("Error:Record index out of writing");
  117. return null;
  118. }
  119.  
  120. public void delete()
  121. {
  122. this.Close();
  123. FileStream fi = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite);
  124. fi.Close();
  125. }
  126. public void write(string _data)
  127. {
  128. if (mode == 'W')
  129. sw.WriteLine(_data);
  130. else MessageBox.Show("Error:File is opened for reading!");
  131.  
  132. }
  133. public void Close()
  134. {
  135. switch(mode)
  136. {
  137. case 'R':
  138. sr.Close();
  139. break;
  140. case 'W':
  141. sw.Close();
  142. break;
  143. }
  144. fs.Close();
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement