Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in my application i want to save the entire document in local system with a agiven filename.
i builted one wpf application with filename mcontents and three comboboxes,on click of a button i want to save the file along with its contents in locla system.iam able to save the file with only name but not its contents ..plz help me
Posted
Updated 6-Dec-11 22:29pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-11 3:57am    
Not a question. What document?! If you want to save it, just do it; you got my approval. :-)
--SA
satishmachineni 7-Dec-11 4:30am    
sir i updated now
LanFanNinja 7-Dec-11 4:40am    
Are you trying to save the contents of a TextBox?? Can you give us an example of the code you are using to try and save the "document"?
satishmachineni 7-Dec-11 4:53am    
private void button1_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = textBox1.Text;

// Default file name
dlg.DefaultExt = ".text";

// Default file extension
dlg.Filter = "Text documents (.txt)|*.txt";
// Filter files by extension
// Show save file dialog box
// Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
// if (result == true)
// {
// Save document
string filename = dlg.FileName;
//write to a local disk
using (FileStream stream = File.Create(@"D:\" + textBox1.Text + ".txt"))


MessageBox.Show("document saved ");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
comboBox1.SelectedValue = "";
comboBox2.SelectedValue = "";



// }

}
satishmachineni 7-Dec-11 4:50am    
yes into a locsystem

1 solution

Try this
In this example "textBox1.Text" is the data I want to save to the file.

C#
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = @"YourFileName.txt"; // Default file name (ex. TextBoxContents.txt)
dlg.DefaultExt = ".text"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
    // Save document
    string fileName = dlg.FileName;

    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName))
    {
        writer.Write(textBox1.Text);
        writer.Close();
    }
}


and of course you will want to modify this for your needs.
 
Share this answer
 
v4
Comments
satishmachineni 7-Dec-11 5:26am    
thanks it is working,thank u for patience
LanFanNinja 7-Dec-11 5:31am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900