Skip to content

Instantly share code, notes, and snippets.

@Plus1XP
Created August 1, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plus1XP/ca5da6432d1e8679115d4438d7d599ca to your computer and use it in GitHub Desktop.
Save Plus1XP/ca5da6432d1e8679115d4438d7d599ca to your computer and use it in GitHub Desktop.
WinForms Open/Save Dialogue Box
private void buttonLoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.Filter = "All Files (*.*)|*.*";
textBoxName.Text = ofd.SafeFileName;
textBoxLocation.Text = ofd.FileName;
if (ofd.ShowDialog() == DialogResult.OK)
{
byte[] saveData = File.ReadAllBytes(ofd.FileName);
//string saveFile = Encoding.Default.GetString(saveData);
string saveFile = Encoding.UTF8.GetString(saveData, 0, saveData.Length);
textBoxContents.Text = saveFile;
}
}
private void buttonSaveFile_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save File";
sfd.Filter = "All Files (*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
string file = textBoxContents.Text;
byte[] data = Encoding.UTF8.GetBytes(file);
File.WriteAllBytes(sfd.FileName, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment