Difference between revisions of "User:Saul/c sharp"
From Organic Design wiki
(Created page with install info) |
(→Reading a File) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | = Install = | + | == Install == |
− | I recommend using mono and [http://www.monodevelop.com monodevelop] for writing, building and running C# projects. | + | I recommend using mono and [http://www.monodevelop.com monodevelop] for writing, building and running C# projects on linux. |
<source lang="bash"> | <source lang="bash"> | ||
sudo apt-get install monodevelop | sudo apt-get install monodevelop | ||
</source> | </source> | ||
Create a new project with Ctrl-Shift-N. | Create a new project with Ctrl-Shift-N. | ||
+ | |||
+ | == Windows Forms == | ||
+ | === Saving a File === | ||
+ | <source lang="c#"> | ||
+ | SaveFileDialog saveFileDialog = new SaveFileDialog(); | ||
+ | |||
+ | saveFileDialog.Filter = "csv File|*.csv"; | ||
+ | saveFileDialog.Title = "Save"; | ||
+ | saveFileDialog.FileName = this.Text; | ||
+ | |||
+ | if (saveFileDialog.ShowDialog() == DialogResult.OK) { | ||
+ | try { | ||
+ | using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { | ||
+ | // get bytes from text you want to save | ||
+ | byte[] data = new UTF8Encoding().GetBytes("data1, data2, data3\ndata1, data2, data3"); | ||
+ | |||
+ | fs.Write(data, 0, data.Length); | ||
+ | fs.Flush(); | ||
+ | } | ||
+ | } catch { | ||
+ | MessageBox.Show("Error saving file"); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | === Reading a File === | ||
+ | <source lang="c#"> | ||
+ | OpenFileDialog dlg = new OpenFileDialog(); | ||
+ | dlg.Filter = "csv files (*.csv)|*.csv"; | ||
+ | |||
+ | if (dlg.ShowDialog() == DialogResult.OK) { | ||
+ | StreamReader sr = new StreamReader(dlg.FileName); | ||
+ | |||
+ | string line = sr.ReadLine(); | ||
+ | while (line != null) { | ||
+ | string[] values = line.Split(','); | ||
+ | |||
+ | // verify there is the correct amount of values... | ||
+ | // if (values.Count == 4) ... | ||
+ | // Do something with them | ||
+ | Console.WriteLine(line + "\n"); | ||
+ | |||
+ | line = sr.ReadLine(); | ||
+ | } | ||
+ | } | ||
+ | </source> |
Latest revision as of 00:50, 7 August 2019
Install
I recommend using mono and monodevelop for writing, building and running C# projects on linux.
sudo apt-get install monodevelop
Create a new project with Ctrl-Shift-N.
Windows Forms
Saving a File
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "csv File|*.csv";
saveFileDialog.Title = "Save";
saveFileDialog.FileName = this.Text;
if (saveFileDialog.ShowDialog() == DialogResult.OK) {
try {
using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) {
// get bytes from text you want to save
byte[] data = new UTF8Encoding().GetBytes("data1, data2, data3\ndata1, data2, data3");
fs.Write(data, 0, data.Length);
fs.Flush();
}
} catch {
MessageBox.Show("Error saving file");
}
}
Reading a File
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "csv files (*.csv)|*.csv";
if (dlg.ShowDialog() == DialogResult.OK) {
StreamReader sr = new StreamReader(dlg.FileName);
string line = sr.ReadLine();
while (line != null) {
string[] values = line.Split(',');
// verify there is the correct amount of values...
// if (values.Count == 4) ...
// Do something with them
Console.WriteLine(line + "\n");
line = sr.ReadLine();
}
}