Skip to content

Instantly share code, notes, and snippets.

@cybrox
Last active August 29, 2015 13:57
Show Gist options
  • Save cybrox/9739665 to your computer and use it in GitHub Desktop.
Save cybrox/9739665 to your computer and use it in GitHub Desktop.
Handle Read and write access
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Cylib {
/**
* @class Cyfile
* @name Cyfile
* @desc Read and write a file
* @author Sven Gehring
* @copyright 2014+ Sven Gehring
*/
public class Cyfile {
private string filepath;
/**
* @method Cyfile
* @name Cyfile class cosntructor
* @desc Set the file path for this file handler
* This will also check if the file exists
* @param {string} filepath - The given file path
*/
public Cyfile(string filepath){
if (!File.Exists(filepath)) {
MessageBox.Show("Invalid file path");
return;
}
this.filepath = filepath;
}
/**
* @method writeString
* @name Write String
* @alias 'schreiben'
* @desc Write a string to the file
* @param {string} input - The given input string
*/
public void writeString(string input) {
FileStream file = new FileStream(this.filepath, FileMode.Create);
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(input);
sw.Close();
file.Close();
}
/**
* @method readString
* @name Read String
* @alias 'lesen'
* @desc Read a string from the file (whole file)
* @return {string} data - The file content
*/
public string readString() {
FileStream file = new FileStream(this.filepath, FileMode.Open);
StreamReader sr = new StreamReader(file);
string data = "";
while (sr.Peek() != -1) data += sr.ReadLine() + "\n";
sr.Close();
file.Close();
return data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment