I have a quick question, how do you save a file in a different format like in "save as"
so far i got this
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
//this saves the file as a text or richtext.
saveFileDialog1.Filter = ("RichText*.rtf; )|*.rtf; |TextDocs *.txt;|*.txt");
saveFileDialog1.FilterIndex = 2;
//this gives the title of the savefiledialog.
saveFileDialog1.Title = "save file";
//this prompts the user if they want to overwrite an existing file.
saveFileDialog1.OverwritePrompt = true;
//gets the input made by the savefiledialog.
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//saves the file.
richTextBox1.SaveFile(saveFileDialog1.FileName,
//saves the text in the richbox
RichTextBoxStreamType.RichText);
I want to be able to save as ether a rtf or a txt format. thanks.
Use filename different name and pass it to SaveFile with the read content buffer from origianl file.
Related
I'll start by noting that I'm a complete beginner to C# and I've been trying to "learn by doing."
The overall project is a media player that I'm creating and augmenting from a couple of YouTube videos I've been watching. I'm at a point where I'd like to understand the SaveFileDialog and how to save items from a ListBox into an xml file.
Before I can get there, however, I'm having trouble with just getting the SaveFileDialog to save anything at all and then close.
Here's the code I'm working with:
public Form1()
{
InitializeComponent();
}
private void btn_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "XML-File | *.xml";
saveFileDialog1.Title = "Save Playlist";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK) ;
}
Right now, when I click the "Save" button (btn_save), the Save dialog will open and it will default to an xml file, but no file will save after clicking Save, and then after clicking Save, the dialog just opens again and again until I select Cancel.
If I try to add
SaveFileDialog.Close();
I get an error saying "SaveFileDialog" does not contain a definition for 'Close', but I figure I've got to set something that tells the dialog to close after hitting Save.
What will help me most here is the "fix" for this and then some comments in the code that explain what's occurring on each line so that I perform more relevant searches and read further.
That said, any help with this would be appreciated. Please let me know if I need to include additional code in this example.
Here's have I would expect code that writes "hello world" to a text file to look like:
private void btn_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text file | *.txt";
saveFileDialog1.Title = "Save Playlist";
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
File.WriteAllText(saveFileDialog1.FileName, "hello world");
}
You can tweak it for XML etc, but the key concepts are:
Show the dialog with ShowDialog() - your code will pause at that point while the dialog is open and resume when it closes
Inspect the result and if it's not OK (i.e. the user clicked Cancel), return without doing anything
Otherwise, do something (like writing a file)
You actually need to save the file with the information obtained from the SaveFileDialog component.
Maybe this could be helpful to you:
public Form1()
{
InitializeComponent();
}
private void btn_save_Click(object sender, EventArgs e)
{
Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "XML-File | *.xml";
saveFileDialog1.Title = "Save Playlist";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
} //end if ShowDialog
} //end Click Button
I have created some code that saves my work to a text file, I was just wondering is there some way so that when I click 'Save' I can read in the text from richTextBox1and set it as the default file name, still with the 'txt' default file extension.
e.g. When I click 'save' the folder dialog comes up and asks you to name your file, just as it would if you were using Word for example, I want that box to already have the text from my richTextBox1 in.
Thanks.
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "C:\\To-Do-List";
save.Filter = "Text Files (*.txt)|*.txt";
save.DefaultExt = ".txt";
DialogResult result = save.ShowDialog();
if (result == DialogResult.OK)
{
using (StreamWriter SW = new StreamWriter(save.FileName))
{
SW.WriteLine(richTextBox1.Text);
SW.WriteLine(richTextBox2.Text);
SW.WriteLine(richTextBox3.Text);
SW.WriteLine(richTextBox4.Text);
SW.WriteLine(richTextBox5.Text);
SW.Close();
}
}
Just set the FileName property on your SaveFileDialog.
Add
save.FileName = String.Format("{0}.txt", richTextBox1.Text);
Before you call ShowDialog.
I have a problem. I have created a temporary .csv file, which contains a lot of data. The directory is a string: path = #"C:\Users\xxxxx\Documents\TempFile.csv";.
I want to save this temporary file as a .csv file, when the user click on the save button. I have created a dialog, so the user is able to choose where to save the file.
private void SaveBT_Click_1(object sender, EventArgs e)
{
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "CSV File|*.csv";
saveDialog.Title = "Gem en CSV fil";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
}
}
}
erm, how about?
File.Copy(tempFileName, saveFialog.FileName);
I want to create a file in a directory selected by the user and named it by the user input.
I tried FolderBrowserDialog but it didn't prompt me to give a file name:
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string path = fbd.SelectedPath;
//string FileName; then concatenate it with the path to create a new file
how can I do that?
You want to create a new file in a folder, so you should:
ask the user to select a folder (with FolderBrowserDialog)
offer the user a way to type a file name, with an input field (separate from the folder dialog)
Then you concat those 2 infos to get your full file name.
Or you can use SaveFileDialog and check if the file already exists when the user has selected a file (with a File.Exists...). There is a property for displaying an alert when the file does not exists, but not on the other side.
So when you got the DialogResult, use File.Exists and you can alert the user.
Sample for this solution:
In this sample (I hope without errors, cannot test right now):
- I open the saveFileDialog on a button called SaveButton with the SaveButton_Click click method
- I have a SaveFileDialog component on my form, called saveFileDialog1. On this component, the event FileOK is associated to my saveFileDialog1_FileOk method
private void SaveButton_Click(object sender, EventArgs e)
{
// Set your default directory
saveFileDialog1.InitialDirectory = #"C:\";
// Set the title of your dialog
saveFileDialog1.Title = "Save file";
// Do not display an alert when the user uses a non existing file
saveFileDialog1.CheckFileExists = false;
// Default extension, in this sample txt.
saveFileDialog1.DefaultExt = "txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// DO WHAT YOU WANT WHEN THE FILE AS BEEN CHOSEN
}
}
// This method handles the FileOK event. It checks if the file already exists
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if (File.Exists(saveFileDialog1.FileName))
{
// The file already exists, the user must select an other file
MessageBox.Show("Please select a new file, not an existing one");
e.Cancel = true;
}
}
This is the code:
private void beginOperationToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFileNameToolStripMenuItem.Enabled = false;
if (File.Exists(fullDefaultDirectory))
{
File.Delete(fullDefaultDirectory);
}
ffmp.Start(fullDefaultDirectory, 25);//"test.avi", #"d:\", 25);
timer1.Enabled = true;
startStop = true;
}
Now i check if the file exist and delete it but thats not good way since i need a file to be on the hard disk.
So what i want to do is:
If the fie exist ask the user if to delete it or run over it with the same name.
If deleted open the savedialog and let the user to set a new file name.
If not deleted just run over the existing file and create the same file name.
This is the savedialog i already have:
private void changeFileNameToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Avi|*.avi";
saveFileDialog1.Title = "Save an Avi File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
//outputFileName = Path.GetFileName(saveFileDialog1.FileName);
//outputDirectory = Path.GetDirectoryName(saveFileDialog1.FileName);
fullDefaultDirectory = saveFileDialog1.FileName;
Options_DB.Set_Video_File(fullDefaultDirectory);
}
}
But im talking about a situation when the user didnt change anything and the variable fullDefaultDirectory contain the same directory and file name then let the user to decide if to delete or run over it .
fullDefaultDirectory always contain a file name since i have a settings file name text file where i save the directory and file name the user selected.
If he didnt select anything the default is some file i did to be default and if he selected a file it will create the file the user selected.
I need to solve the case where the file is already exist.
The SaveFileDialog class already covers this with the OverwritePrompt property, which will cause the dialog to ask the user if they're sure they want to overwrite an existing file.