This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
203 lines (187 loc) · 8.41 KB
/
Form1.cs
File metadata and controls
203 lines (187 loc) · 8.41 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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.Threading.Tasks;
using System.Windows.Forms;
namespace ReadFromName
{
public partial class frm_main : Form
{
public frm_main()
{
InitializeComponent();
}
string folderName = @"H:\Students_Folder\Charlie Hatch\Semesters\Multi-Semester\App Dev\Projects\App Dev 2\ReadFromName\";
string fileName = "file";
string fileExtension = ".txt";
string initialPath = string.Empty;
string path = string.Empty;
const string errorMessage = "I'm sorry dave, I'm afraid I can't do that";
private void frm_main_Load(object sender, EventArgs e)
{
initialPath = folderName + fileName + fileExtension;
updatePath(true);
txt_manualPath.Text = path; //ensure box starts with the default path
} //initialization code
#region messages
private void error(string message, Exception ex = null, bool revealException = false)
{
string msg = message; //what message to display
if (revealException) //put the toggle
{
msg = msg + ": " + ex.Message; //the message
}//a bool to toggle if the exception code should be included in the error message
MessageBox.Show(msg); //show the message
} //this is for error messages. the text is mandatory, but the
//rest isn't necessary. if you don't provide an exception, it won't display one. If you provide an error, it will default to "hide error".
// WARNING: If you provide a boolean, you can controll if it's hidden. If you provide a boolean but no error, well, it won't work righ
#endregion messages
#region fileAcess
private void updatePath(bool reset = false)
{
if (reset)
{
path = initialPath;
}
else
{
path = txt_manualPath.Text;
}
txt_curPath.Text = path;
}
private string fullPath()
{
string fullPath = string.Empty;
if (path != string.Empty)
{
fullPath = path; //use the path
}
else
{
fullPath = initialPath; //if the fullpath was somehow path is blank, use pre-set
}
return fullPath; //this part outputs them
} //full path (requires no input, it just combines the 3 pre-set variables). Due to method overloading, this is the "same" method as the one
// that checks for the files existance. if you mathch this ones paramaters (meaning no input), it will do this one. Input a boolean, and it will match the other, and therefore check
//for it's existance
private bool fullPath(bool confirm)
{
if (confirm) //checks if confirm has an input, then checks if it's true
{
bool status = File.Exists(fullPath()); //if it is true, it will proceed to check if the file defined by fullPath exists
if (status)
{
return true; //if it exists, it outputs trye
} //a boolean representing wether or not the file exists
else
{
error(errorMessage); //displays the error message about haveing no file
// probally would be best if I told them the reason, but where's the fun in that?
//this allows the message to be trigered as part of the check, so the if that's within the button doesn't need it
return false; //outputs the false, as it doesn't exist
} //no file
}// wether to confirm or not. if it should, it does this code
else
{
return false; //if the input is false, it doesn't check, and outputs a false
} //don't check
} //confirming the file
// I used the same method as the one to make the path, and a feature I found called "method overloading". basicaly, by making a method with different paramaters
//it will run the method that I put the paramaters in. So, if I don't put in anything (the first method), it will output that string. But, if I put in a boolean (like this one)
//then it will preform the check
#endregion
#region UI
private void btn_readAllText_Click(object sender, EventArgs e)
{
if (fullPath(true))
{
try //double check
{
string text = File.ReadAllText(fullPath()); //read the file
lst_readAllText.Items.Clear(); //clear the list
lst_readAllText.Items.Add(text); //add the contents to the list box
}
catch (Exception ex) //if there's an error
{
error(errorMessage, ex); //displays the eror message, but due to the optional value, never displays the error the computer threw (however it
//is still acessable, as it's acessible, since it's a higher level variable
} //double check
} //check for files
} //read all the text in the file
private void btn_readAllLine_Click(object sender, EventArgs e)
{
if (fullPath(true)) //check if file exists
{
lst_readAllLine.Items.Clear(); // clear the list to prevent duplicates, which are a tad annoying
try //double check
{
string[] contentArray = File.ReadAllLines(fullPath()); //get the full lines output
foreach (string item in contentArray) //repeat with each
{
lst_readAllLine.Items.Add(item); //add item to list box
} //repeat each individual line to add it
}
catch (Exception ex) //check for error
{
error(errorMessage, ex); //display error if it somehow manages to not work even if the file is there
} //double check
} //check if file exists
} //read all the lines in the file
private void btn_streamReader_Click(object sender, EventArgs e)
{
if (fullPath(true)) //check if file exists
{
try //double check
{
using (StreamReader sr = new StreamReader(fullPath())) //creates a seperate instance of StreamReader with the file name built-in
{
lst_streamReader.Items.Clear(); //clears the listbox
while (!sr.EndOfStream) //add each line to it one by one
{
string line = sr.ReadLine(); //gets the next line of text from the file
lst_streamReader.Items.Add(line); //adds the item to the list
}
}
}
catch (Exception ex) //check for error
{
error(errorMessage, ex); //display error message
}
}
}
private void btn_close_Click(object sender, EventArgs e) //close
{
Application.Exit(); //exit the program
}
private void btn_triggerAll_Click(object sender, EventArgs e)
{
btn_readAllLine_Click(sender, e); //triger read all lines
btn_readAllText_Click(sender, e); //triger read all text
btn_streamReader_Click(sender, e); //trigger the string reader
} //triger all the buttons
#endregion UI
private void btn_clearIn_Click(object sender, EventArgs e)
{
txt_manualPath.Text = string.Empty; //clear input
updatePath();
txt_curPath.Text = fullPath();
} //clear textbox
private void btn_input_Click(object sender, EventArgs e)
{
updatePath();
}
private void btn_showCurPath_Click(object sender, EventArgs e)
{
txt_manualPath.Text = txt_curPath.Text;
}
private void btn_default_Click(object sender, EventArgs e)
{
updatePath(true);
}
}
}