How to create a basic save/open file dialog in Visual C#?

I am creating a simple Sticky Notes application that will open/read/save text files (in a *.sticky) extention. I was able to code the entire application, except for the save, save as, and open commands. Does anyone know how to do this?

Thanks.

nevdull5 years ago
Visual C# makes this easy.

using System.IO;

// The button callback for the "Save Notes" button

private void OnSaveFileButtonClicked(object sender, System.EventArgs e) 
{ 
  // Configure savefile dlg with your stuff
  SaveFileDialog saveFileDlg = SaveFileDialog();
  // Convenient if you already have a application directory to store notes in
  saveFileDlg.InitialDirectory = "%USERPROFILE%\\Documents";
  saveFileDlg.Filter = "Sticky Note File (*.sticky)|*.sticky|All Files (*.*)|*.*";
  // it's nice to use the folder that the user has been using if any
  saveFileDlg.RestoreDirectory = true;
  
  // present the dialog to the user
  if (saveFileDlg.ShowDialog() == DialogResult.OK && saveFileDlg.FileName != "")
  {
    // now the user has dismissed the dialog so check for their choices
    using (FileStream ostr = saveFileDlg.OpenFile())
      if (ostr != null)
      {
        ostr.Write(noteTextArea.Text, 0, noteTextArea.TextLength);

      }
  }
} 

// callback for the "Open Sticky Notes" button
// it's similar to above so no big surprises here
private void OnOpenStickyNote(object sender, System.EventArgs e)
{
  Stream ostr = null;
  OpenFileDialog openFileDlg = new OpenFileDialog();
  
  // again, be nice and setup the dialog for your user
  openFileDlg.InitialDirectory = "%USERPROFILE%";
  openFileDlg.Filter = "Sticky Notes (*.sticky)|*.sticky|All Files (*.*)|*.*";
  openFileDlg.RestoreDirectory = true;

  if (openFileDlg.ShowDialog() == DialogResult.OK)
  {
    if ((ostr = openFileDlg.OpenFile()) != null)
    {
       using(ostr)
       {
          // now open the file, parse it, or do whatever you need to here
       }
    }
  }
}
That should be about it. Hope this helps! -gian
red_green5 years ago
That seems like a lot of code for a save/open function
it might be able to be simplified
kieutu5 years ago
I have learn C# for 2 years, i can help you, it's really easy, this is the code:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
if (kiemtrasave == 2)
{
result = MessageBox.Show("Click Yes to Save or Click No to Exit", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile(openFileDialog1.FileName);
this.Text = cap + ": " + openFileDialog1.FileName;
}
else if(result == DialogResult.Yes)
{
if (path != "")
{
saveFileDialog1.FileName = path;
richTextBox1.SaveFile(saveFileDialog1.FileName);
kiemtrasave = 0;
this.Text = cap + ": " + saveFileDialog1.FileName;
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
path = openFileDialog1.FileName;
kiemtrasave = 0;
}
this.Text = cap + ": " + openFileDialog1.FileName;
}
else
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
kiemtrasave = 0;
}
this.Text = cap + ": " + saveFileDialog1.FileName;
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
kiemtrasave = 0;
}
this.Text = cap + ": " + openFileDialog1.FileName;
}
}
}
else
{
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
path = openFileDialog1.FileName;
this.Text = cap + ": " + openFileDialog1.FileName;
kiemtrasave = 0;
}
}
}

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
if (kiemtrasave == 2)
{
result = MessageBox.Show("Click Yes to Save or Click No to Create a New text", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result== DialogResult.No)
{
richTextBox1.Clear();
this.Text = cap;
kiemtrasave = 0;
}
else if(result == DialogResult.Yes)
{
if (path != "")
{
saveFileDialog1.FileName = path;
richTextBox1.SaveFile(saveFileDialog1.FileName);
this.Text = cap + ": " + saveFileDialog1.FileName;
richTextBox1.Clear();
this.Text = cap;
kiemtrasave = 0;
}
else
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
kiemtrasave = 0;
}
path = saveFileDialog1.FileName;
this.Text = cap + ": " + saveFileDialog1.FileName;
}
}
}
else
{
path = "";
richTextBox1.Clear();
this.Text = cap;
kiemtrasave = 0;
}
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (path != "")
{
saveFileDialog1.FileName = path;
richTextBox1.SaveFile(saveFileDialog1.FileName);
this.Text = cap + ": " + saveFileDialog1.FileName;
kiemtrasave = 0;
}
else
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
kiemtrasave = 0;
}
}
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
kiemtrasave = 0;
}
path = saveFileDialog1.FileName;
this.Text = cap + ": " + saveFileDialog1.FileName;
}

this application for my examination
note: kiemtrasave is variable that you check the richtextbox saved or not
0 is saved.