Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public class File
- {
- FileStream fs = null;
- private StreamReader sr = null;
- private StreamWriter sw = null;
- private int recsize;
- private long filesize;
- private char mode;
- private string filepath;
- public File()
- {
- }
- public long RecNum
- {
- get
- {
- return filesize / recsize;
- }
- }
- private bool checkfile(string _filepath)
- {
- int fileindex = _filepath.LastIndexOf("\\", _filepath.Length);
- string file = _filepath.Substring(fileindex + 1);
- string directorypath = _filepath.Substring(0, fileindex + 1);
- filepath = _filepath;
- if (Directory.Exists(directorypath))
- {
- string[] filenames = Directory.GetFiles(directorypath);
- foreach (string s in filenames)
- if (s.Substring(fileindex + 1) == file) return true;
- MessageBox.Show("New file is created");
- return true;
- }
- else
- {
- MessageBox.Show("Directory doesn't exist");
- return false;
- }
- }
- public bool open(string _filepath, char _mode, int _recsize)
- {
- mode = _mode;
- recsize = _recsize;
- if (checkfile(_filepath))
- {
- switch (mode)
- {
- case 'C':
- fs = new FileStream(_filepath, FileMode.Create, FileAccess.ReadWrite);
- break;
- case 'R':
- fs = new FileStream(_filepath, FileMode.OpenOrCreate, FileAccess.Read);
- break;
- case 'W':
- fs = new FileStream(_filepath, FileMode.OpenOrCreate, FileAccess.Write);
- break;
- default:
- MessageBox.Show("Error: File Access Mode(W or R)");
- return false;
- }
- filesize = fs.Length;
- try
- {
- switch (mode)
- {
- case 'R':
- sr = new StreamReader(fs);
- break;
- case 'W':
- sw = new StreamWriter(fs);
- break;
- }
- }
- catch (Exception Ex)
- {
- MessageBox.Show(Ex.Message);
- return false;
- }
- return true;
- }
- return false;
- }
- public string read(int _recordnum)
- {
- if (mode == 'R')
- {
- if ((filesize / recsize) >= _recordnum)
- {
- int startP = _recordnum * recsize;
- fs.Position = startP;
- return sr.ReadLine();
- }
- MessageBox.Show("Error:Record index out of bound!");
- }
- else MessageBox.Show("Error:Record index out of writing");
- return null;
- }
- public void delete()
- {
- this.Close();
- FileStream fi = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite);
- fi.Close();
- }
- public void write(string _data)
- {
- if (mode == 'W')
- sw.WriteLine(_data);
- else MessageBox.Show("Error:File is opened for reading!");
- }
- public void Close()
- {
- switch(mode)
- {
- case 'R':
- sr.Close();
- break;
- case 'W':
- sw.Close();
- break;
- }
- fs.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement