-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
124 lines (113 loc) · 4.52 KB
/
Program.cs
File metadata and controls
124 lines (113 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.IO;
using System.Threading;
namespace CommentEraser
{
class CommentEraser {
private static string Path;
private static OutcomeOptions Options;
private static FileStream FileStream;
public CommentEraser(string pathToFile, OutcomeOptions options)
{
// SayHello();
Path = pathToFile;
Options = options;
FileStream = File.Open(Path,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
}
/* private void SayHello() {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(@"
_____ __ ____
/ ______ __ _ __ _ ___ ___ / /_ / _________ _______ ____
/ /__/ _ \/ ' \/ ' / -_/ _ / __/ / _// __/ _ `(_-/ -_/ __/
\___/\___/_/_/_/_/_/_\__/_//_\__/ /___/_/ \_,_/___\__/_/");
Thread.Sleep(80);
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(@"
_____ __ ____
/ ______ __ _ __ _ ___ ___ / /_ / _________ _______ ____
/ /__/ _ \/ ' \/ ' / -_/ _ / __/ / _// __/ _ `(_-/ -_/ __/
\___/\___/_/_/_/_/_/_\__/_//_\__/ /___/_/ \_,_/___\__/_/");
Thread.Sleep(80);
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(@"
_____ __ ____
/ ______ __ _ __ _ ___ ___ / /_ / _________ _______ ____
/ /__/ _ \/ ' \/ ' / -_/ _ / __/ / _// __/ _ `(_-/ -_/ __/
\___/\___/_/_/_/_/_/_\__/_//_\__/ /___/_/ \_,_/___\__/_/");
Console.ResetColor();
}
*/
public void Erase(string sequenceStart) {
StreamReader reader = new StreamReader(FileStream);
string newFileName = new FileInfo(Path).DirectoryName + "\\" + new Random().Next() + ".txt";
StreamWriter writer = new StreamWriter(File.Create(newFileName));
string currentString = reader.ReadLine();
while (currentString!=null) {
if (currentString.IndexOf(sequenceStart)==0) { }
else if (currentString.Contains(sequenceStart))
{
writer.WriteLine(currentString.Substring(0, currentString.IndexOf(sequenceStart)));
}
else{
writer.WriteLine(currentString);
}
currentString = reader.ReadLine();
}
reader.Close();
writer.Close();
FileStream.Close();
writeDown(newFileName);
}
private void writeDown(string newFileName) {
if (Options == OutcomeOptions.updateCurrent)
{
File.Delete(Path);
File.Move(newFileName, System.IO.Path.GetFullPath(Path));
}
else if (Options == OutcomeOptions.createNew)
{
File.Move(newFileName, System.IO.Path.GetDirectoryName(Path) + System.IO.Path.DirectorySeparatorChar + new Random().Next() + System.IO.Path.GetExtension(Path));
}
}
public void Erase(string startingSequence,string endingSequence) {
StreamReader reader = new StreamReader(FileStream);
string newFileName = new FileInfo(Path).DirectoryName + "\\" + new Random().Next() + ".txt";
StreamWriter writer = new StreamWriter(File.Create(newFileName));
string entireText = reader.ReadToEnd();
if (entireText.Contains(startingSequence) && entireText.Contains(endingSequence))
{
while (entireText.Contains(startingSequence))
{
entireText = entireText.Substring(0, entireText.IndexOf(startingSequence)) +
entireText.Substring(entireText.IndexOf(endingSequence) + endingSequence.Length);
}
writer.WriteLine(entireText);
}
else {
Console.WriteLine("The text doesn't contain specified starting or ending sequence");
reader.Close();
writer.Close();
FileStream.Close();
File.Delete(newFileName);
return;
}
reader.Close();
writer.Close();
FileStream.Close();
writeDown(newFileName);
}
}
enum OutcomeOptions {
createNew = 0,
updateCurrent
}
class Program
{
static void Main(string[] args)
{
}
}
}