C# Basic Notepad

Difficulty in saving file through Savefiledialog

Page 1 of 1

7 Replies - 3951 Views - Last Post: 08 March 2010 - 02:40 AM Rate Topic: -----

#1 rajatarora10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 10

C# Basic Notepad

Posted 05 March 2010 - 02:05 AM

I am working on basic notepad application. In the new option of menu strip when I want to save changes to the code by typing filename in the savedialog box , the file is not saved and exception occurs.plz help to get my control working. My code is as following.
else if (result == DialogResult.Yes)
                {  
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.InitialDirectory = "C:\\store";
                    saveFileDialog1.FileName =  "";
                    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                    saveFileDialog1.FilterIndex = 2;
                    saveFileDialog1.RestoreDirectory = true;

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        System.IO.File.WriteAllText(@"C:\\store", textBox1.Text);
                    }
               }


Is This A Good Question/Topic? 0
  • +

Replies To: C# Basic Notepad

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: C# Basic Notepad

Posted 05 March 2010 - 02:10 AM

First off please :code:. Secondly, from what I can see with the little code you provided, you are initializing the dialog after the user clicks okay, there for erasing everything the user had up to that point entered.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "C:\\store";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;



That whole chunk needs to appear before the SaveFileDialog1 is shown to the user.
Was This Post Helpful? 0
  • +
  • -

#3 rajatarora10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 10

Re: C# Basic Notepad

Posted 05 March 2010 - 04:22 AM

The advice you gave me is working , but still a problem persists. Time and agai an error pops up that access to path is denied when I try to store the file in the location given in the code above. plz help
Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: C# Basic Notepad

Posted 05 March 2010 - 04:57 AM

saveFileDialog.Title = "Save File as. . ."; // adds a title to the dialog
saveFileDialog.OverwritePrompt = true; // Asks the user if they want to overwrite the file if it exists
saveFileDialog.ValidateNames = true; // makes sure the name is a proper file name for windows
saveFileDialog.AutoUpgradeEnabled = true; // property needed for Vista
saveFileDialog.CreatePrompt = true; // Prompts the user to create the file if it is not already created
saveFileDialog.CheckPathExists = true; // Checks to make sure the user is in a valid path



Try these properties out with it and let me know if you still have the problem.
Was This Post Helpful? 1
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# Basic Notepad

Posted 05 March 2010 - 05:45 AM

View Postrajatarora10, on 05 March 2010 - 06:22 AM, said:

The advice you gave me is working , but still a problem persists. Time and agai an error pops up that access to path is denied when I try to store the file in the location given in the code above. plz help
Thanks in advance


can you please post your entire code for the Save button?
Was This Post Helpful? 1
  • +
  • -

#6 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: C# Basic Notepad

Posted 05 March 2010 - 06:27 AM

View Postrajatarora10, on 05 March 2010 - 02:05 AM, said:

I am working on basic notepad application. In the new option of menu strip when I want to save changes to the code by typing filename in the savedialog box , the file is not saved and exception occurs.plz help to get my control working. My code is as following.
else if (result == DialogResult.Yes)
                {  
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.InitialDirectory = "C:\\store";
                    saveFileDialog1.FileName =  "";
                    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                    saveFileDialog1.FilterIndex = 2;
                    saveFileDialog1.RestoreDirectory = true;

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        System.IO.File.WriteAllText(@"C:\\store", textBox1.Text);
                    }
               }

In your code, you're trying to save it to 'C:\store'.
Change:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        System.IO.File.WriteAllText(@"C:\\store", textBox1.Text);
                    }

to:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        System.IO.File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);
                    }

Hope that helps and good luck!

This post has been edited by Amrykid: 05 March 2010 - 06:41 AM

Was This Post Helpful? 2
  • +
  • -

#7 rajatarora10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 10

Re: C# Basic Notepad

Posted 08 March 2010 - 01:06 AM

Thanks Man This really worked !!!!!!!!!!!!

Thanks dude the application seems tobe in better working mode now!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#8 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: C# Basic Notepad

Posted 08 March 2010 - 02:40 AM

I think the issue was you were trying to save the text to a file path rather than an actual file!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1