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);
}
}
C# Basic NotepadDifficulty in saving file through Savefiledialog
Page 1 of 1
7 Replies - 2426 Views - Last Post: 08 March 2010 - 02:40 AM
#1
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.
Replies To: C# Basic Notepad
#2
Re: C# Basic Notepad
Posted 05 March 2010 - 02:10 AM
First off please
. 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.
That whole chunk needs to appear before the SaveFileDialog1 is shown to the user.
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.
#3
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
Thanks in advance
#4
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.
#5
Re: C# Basic Notepad
Posted 05 March 2010 - 05:45 AM
rajatarora10, 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
Thanks in advance
can you please post your entire code for the Save button?
#6
Re: C# Basic Notepad
Posted 05 March 2010 - 06:27 AM
rajatarora10, 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
#7
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!!!!!!!!!!
Thanks dude the application seems tobe in better working mode now!!!!!!!!!!
#8
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!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|