Monday, November 23, 2015
Data input & save & output (using a .txt file)
using System;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
public class Sample2 {
public static void Main(string[] args) {
bool isSecond = false;
bool shouldStop = false;
string path = "";
string text = "";
while(!shouldStop){
try{
if(!isSecond){
Console.WriteLine("Prepare a .txt file to write on.");
Console.WriteLine("Give me a directory for the .txt file.");
Console.WriteLine("(Example: C:\\Users\\Documents\\aaa.txt)");
Console.WriteLine("To stop this program, write [/stop] and hit the enter key.");
path = Console.ReadLine();
}
if(path.Equals("/stop", StringComparison.CurrentCultureIgnoreCase)){
break;
}
Encoding enc = Encoding.GetEncoding("Shift_JIS");
//Shift JIS is for Japanese. Please change here depending on your language.
StreamReader sr = new StreamReader(path, enc);
text = sr.ReadToEnd();
sr.Close();
Console.WriteLine("\nTo stop this program, write [/stop] and hit the enter key.");
Console.WriteLine("If you want to open the .txt file, write [/open] and hit the enter key.");
Console.WriteLine("If you want to check the inside of the ,txt file, write [/view] and hit the enter key.");
Console.WriteLine("If you want to search for a certain word, write [/search] and hit the enter key");
Console.WriteLine("Write some words to save in the .txt file:\n");
StreamWriter writer = new StreamWriter(@path, true, enc);
string word = Console.ReadLine();
if(word.Equals("/stop", StringComparison.CurrentCultureIgnoreCase)){
writer.Close();
break;
}else if(word.Equals("/open", StringComparison.CurrentCultureIgnoreCase)){
writer.Close();
System.Diagnostics.Process.Start(@path);
isSecond = true;
Console.WriteLine("Excuted.");
continue;
}else if(word.Equals("/search", StringComparison.CurrentCultureIgnoreCase)){
writer.Close();
Console.WriteLine("What word do you want to search for?");
string searchWord = Console.ReadLine();
Console.WriteLine(searchWord + " is being searched for..");
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(
@searchWord,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Match m = r.Match(text);
int count = 0;
while (m.Success){
count++;
m = m.NextMatch();
}
Console.WriteLine("The word " + searchWord + "appreas " + count + " times in the .txt file.");
isSecond = true;
continue;
}else if(word.Equals("/view", StringComparison.CurrentCultureIgnoreCase)){
writer.Close();
Console.WriteLine("Saved data: \n" + text + "\n");
isSecond = true;
continue;
}
writer.WriteLine(word);
Console.WriteLine("Saved");
writer.Close();
isSecond = true;
}catch(FileNotFoundException){
Console.WriteLine("File not found");
return;
}catch(Win32Exception){
Console.WriteLine("File not found");
return;
}catch(IOException){
Console.WriteLine("IOException");
return;
}catch(NotSupportedException){
Console.WriteLine("NotSupportedException");
return;
}catch(ArgumentException){
Console.WriteLine("ArgumentException");
return;
}
}
Console.WriteLine("Program terminated");
if(isSecond){
Console.WriteLine(".txt file will be opened.");
System.Diagnostics.Process.Start(@path);
}
return;
}
}