Difference between revisions of "User:Saul/c sharp"
From Organic Design wiki
m (→Saving a File) |
m (→Saving a File) |
||
Line 12: | Line 12: | ||
saveFileDialog.Filter = "csv File|*.csv"; | saveFileDialog.Filter = "csv File|*.csv"; | ||
− | saveFileDialog.Title = "Save | + | saveFileDialog.Title = "Save"; |
saveFileDialog.FileName = this.Text; | saveFileDialog.FileName = this.Text; | ||
Line 19: | Line 19: | ||
using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { | using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { | ||
// get bytes from text you want to save | // get bytes from text you want to save | ||
− | byte[] data = new UTF8Encoding().GetBytes( | + | byte[] data = new UTF8Encoding().GetBytes("data1, data2, data3\ndata1, data2, data3"); |
fs.Write(data, 0, data.Length); | fs.Write(data, 0, data.Length); |
Revision as of 00:42, 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");
}
}