Bit of an odd one here , I'm writing an app which gives a save file option , the save file dialog is coded up as normal
SaveFileDialog ofd = new SaveFileDialog();
the dialog box comes up no problem and clicking save doesn't throw up any errors however no file is saved and I'm not sure why , any ideas ? I've googled it and can't find a similar problem
The SaveFileDialog class doesn't save anything, it prompts the user to choose a location and a file name to save the file. It is your job to save the file
This example extracted from the MSDN link above explains the concept
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
}
Stream stream;
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
ofd.FilterIndex = 2 ;
ofd.RestoreDirectory = true ;
if(ofd.ShowDialog() == DialogResult.OK)
{
if((stream = ofd.OpenFile()) != null)
{
//FileStream might be better for you but since i don't know what you write, this will serve as an example
stream.Write(bytes,offset,count);
stream.Close();
}
Related
I am trying to use a OpenFileDialog to let the user select a local file that can interact with my app. The file can be in use when the user selects it as I really only want to get the file's path. My issue is that if I try doing this with a OpenFileDialog, when the user presses "open", it actually tries to open the file even though I don't call for opening anywhere. Since the file is in use, it throws an error.
Is there any way for me to present a dialog for a user to just select a file an do nothing else?
Heres my current code:
DialogResult result = this.qbFilePicker.ShowDialog();
if (result == DialogResult.OK)
{
string fileName = this.qbFilePicker.FileName;
Console.WriteLine(fileName);
}
Make sure you are using
using System.Windows.Forms;
Then the following code from MSDN will not open your file unless you explicitly uncomment the bit that opens the file :)
(Take out the bits that don't pertain to you such as the .txt filter etc)
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
Console.WriteLine(fileName);
//to open the file
/*
try
{
Stream myStream = null;
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
* */
}
}
I am trying to read a .txt file into a multi-line text box with the following code. I have gotten the file dialog button to work perfectly, but I am not sure how to get the actual text from the fiile into the textbox. Here is my code. Can you help?
private void button_LoadSource_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
if you just need the complete text, you should use the function File.ReadAllText - pass it the FileName/Path selected in the dialoge (openFileDialog1.FileName).
to load for example the content into a textbox, you can write:
textbox1.Text = File.ReadAllText(openFileDialog1.FileName);
opening and using streams is a little bit more complicated, for that you should look up the using - statement
here is the code I am currently using to open a file using the openfiledialog `
private void openToolStripMenuItem_Click_1(object sender, System.EventArgs e)
{
//opens the openfiledialog and gives the title.
openFileDialog1.Title = "openfile";
//only opens files from the computer that are text or richtext.
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
//gets input from the openfiledialog.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//loads the file and puts the content in the richtextbox.
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
richTextBox1.Text = (sr.ReadToEnd());
sr.Close();` here is the code I am using to save through a savefiledialog `
Stream mystream;
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((mystream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(mystream);
wText.Write("");
mystream.Close();
`
It allows me to open text files but I can't save changes nor create my own text file. no errors are shown during run time. Thanks again for the extra help.
The SaveFileDialog doesn't do the actual saving for you; it simply allows the user to specify a file path. You use the file path and then do the heavy lifting with an implementation of the StreamWriter class, something like:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using( Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew) )
using( StreamWriter sw = new TextWriter( s ) )
{
sw.Write( someTextBox.Text );
}
}
How do I get the SaveFileDialog to pop up and redirect to a selected folder? I have tried what I can but I've only established to get the program to open the File Explorer and redirect to the folder without the SaveFileDialog working.
This is my code:
private void button9_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
myStream.Close();
}
}
}
Just set your initial directory
For example:
saveFileDialog1.InitialDirectory = #"C:\Users\<username>\Desktop";
I've got a save dialog box which pops up when i press a button. However i dont want to save a file at that point, i want to take the name and place it in the text box next to the button, for the name to be used later.
Can anybody tell me how to obtain the file path from the save dialog box to use it later?
Here is a sample code I just wrote very fast... instead of Console.Write you can simply store the path in a variable and use it later.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
}
Addressing the textbox...
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = saveFileDialog.FileName;
}
private void mnuFileSave_Click(object sender, EventArgs e)
{
dlgFileSave.Filter = "RTF Files|*.rtf|"+"Text files (*.txt)|*.txt|All files (*.*)|*.*";
dlgFileSave.FilterIndex = 1;
if (dlgFileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK && dlgFileSave.FileName.Length > 0)
{
foreach (string strFile in dlgFileSave.FileNames)
{
SingleDocument document = new SingleDocument();
document.rtbNotice.SaveFile(strFile, RichTextBoxStreamType.RichText);
document.MdiParent = this;
document.Show();
}
}
}
Try below code.
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);