SaveFileDialog saves as blank ext? - C# - c#

So I got the hang of doing OpenFileDialog, now I can't seem to understand SaveFileDialog. Looked at a few pages and each of them have there own ways of doing it, but none of them get down to the point of saving the text that is in the richtextbox to a file.
private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog exportdialogue = new SaveFileDialog();
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
exportdialogue.FilterIndex = 2;
exportdialogue.RestoreDirectory = true;
if (exportdialogue.ShowDialog() == DialogResult.OK)
{
if ((myStream = exportdialogue.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write("Some Text");
myStream.Close();
}
}
}
Using a richtextbox, and a normal button, also "using System.IO;" (For the Stream )
I am trying to get the button to use SaveFileDialog so it can export the content within the richtextbox to a text file.
Issue:
Unsure what I need to do from here to make it save contents from the rich text box.
Don't know why SaveFileDialog saves files with no extension when a filter is in place.

You set:
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
which only contains ONE filter, but you set:
exportdialogue.FilterIndex = 2;
which means to use the SECOND filter. (FilterIndex is 1-based).
If you set FilterIndex = 1, your file should have the extension .txt

you can use using {} block to solve the issue:
Try this:
SaveFileDialog exportdialogue = new SaveFileDialog();
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
exportdialogue.FilterIndex = 2;
exportdialogue.RestoreDirectory = true;
if (exportdialogue.ShowDialog() == DialogResult.OK)
{
using( Stream myStream = exportdialogue.OpenFile())
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write("Some Text");
wText.Close();
}
}

you need to use savefile method of reach textbox and pass to it the filename from savedialogbox.
reachtextbox.SaveFile(exportdialogue.FileName);
ps: it will be something like above code.

Related

C# Multiline textBox to .TXT file

I'm definitely using the wrong method. Actually I'm trying to convert from JAVA to C# and it's beginning to become tough ..
Anyway, I have a textBox1 that is multiline, I write to it by for looping an ArrayList.
The textbox1 looks like this:
Website: https://google.dk
Firmanavn: Google LLC
Email: google#gmail.com
CVR: 123456
gScore: 1
gLink: googlePageSpeedLink
The code that I use right now, which manages to create a file, but it ends up empty. I am surely doing something wrong, and I'm unsure how to write the textBox to the file.
private void button3_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
String path = Path.GetFullPath(saveFileDialog1.FileName);
path = DialogResult.ToString();
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
File.WriteAllText(path, textBox1.Text);
myStream.Close();
}
}
}
Any help appreciated... :3 :3 I searched for around 1½ hours on StackOverFlow but I didn't manage to see a sample that would match my way of code :3
I hope it's not a duplicate question, thanks a lot for your answers :)
You don't need the Stream:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = Path.GetFullPath(saveFileDialog1.FileName);
File.WriteAllText(path, textBox1.Text);
}

C# - I'm having problems overwriting data in text file from Listbox

I'm creating a small program that can save website urls/links into a listbox. From there, I can save the contents of the listbox onto a text file. That text file is then saved to a folder on my desktop that was prematurely made for the program. The application can open a text file and display the contents to the listbox as well as create and save a new text file with. My problem is how would I overwrite the text file properly.
This is the code I have for the Save Toolbox button:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string line = "";
if (File.Exists(path)) //the path string is a folder on my desktop
{
FileStream fstream = File.Open(path, FileMode.Create);
while (line != null)
{
using (StreamWriter write = new StreamWriter(fstream))
{
foreach (object item in WebListBox.Items)
write.WriteLine(item.ToString());
}
}
}
else
{
saveFileDialog1.Filter = "Text files (*.txt)|*.txt";
saveFileDialog1.Title = "Save as Text File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream S = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
{
using (StreamWriter st = new StreamWriter(S))
{
foreach (var items in WebListBox.Items)
st.WriteLine(items.ToString());
}
}
}
}
}
The problem is that after I create the text file using the Save As ToolBar button, the compiler seemingly ignores the code in my if statement and assumes that I want to always create a new text file to save to. Thanks in advance!
After multiple revisions and outside help, I figured out the problem. The file name I was trying to retrieve from my folder directory was null, so nothing would be saved. Therefore what I did was I would retrieve the file name from the form itself if it was last opened/saved onto the form.
Here's a picture to help visualize. Any text file that was last opened or saved, it would display it here and compiler would use that path in my code.
Using the full path of the file instead of the shorten one, I would overwrite the contents of the file to that path. Doing this also allows me save my files anywhere on my computer.
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
var fileNameAndPath = filedisplaylabel.Text;
if (string.IsNullOrEmpty((fileNameAndPath)))
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filedisplaylabel.Text = Path.GetFullPath(openFileDialog1.FileName);
fileNameAndPath = filedisplaylabel.Text;
}
}
try
{
using (FileStream fstream = new FileStream(fileNameAndPath,
FileMode.Open,
FileAccess.Write))
{
using (StreamWriter write = new StreamWriter(fstream))
{
foreach (var item in WebListBox.Items)
write.WriteLine(item.ToString());
write.Close();
}
fstream.Close();
}
}
catch (DirectoryNotFoundException ex)
{
MessageBox.Show(ex.Message, "File not found error");
}
}

How could I make this set of code save the user input into another folder?

I am so lost.... how could I make this set of code save the user input into another folder? (Im trying to save .png files if you need to know that)
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)
{
myStream.Close();
}
}
}
There is also some other and or additional code that you would really need to write for example if the user selects a file from one folder, then you could just capture that FilePath using System.IO and look up how to use GetFilePath , FileName, etc... then once the file is selected that they want to save ..why not use the File.Copy() method. there are plenty of additional examples online here on SO as well
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using( Stream myStream = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using( StreamWriter sw = new TextWriter(myStream) )
{
sw.Write("here you can write lines from a file that you read or you can simple write what ever text you are wanting to save to a file this should help you get started" );
}
}
Here is an even simpler way of doing it
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.File.Copy(saveFileDialog1.Filename, "some dest filePath");
}

saving data from a query as a csv file

I've got an application which opens a csv file and displays all the contents into a formatted datagridview. From there I have a button which opens another form that contains a series of checkboxes. The check boxes have all the attributes of the csv file we opened before, and the user is supposed to be able to query the file based on witch attributes they want, then save the file.
For example, if they only want a file which displays all the entries for animals with wings, they select the wings check box only. From there, you select the save button and it's supposed to save the file.
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
const string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
StreamWriter writer = null;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
writer.WriteLine(header);
foreach (Animal animal in animalQuery)
{
writer.Write(animal);
}
writer.Close();
}
}
This is the code for the save button, but there are errors under:
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
I'm not sure why.
unless your code is exact, you cannot assign to a constant variable for your code saying:
filter = saveFileDialog1.FileName;
You declared "filter" as a constant variable further up:
const string filter = "CSV file (.csv)|.csv| All Files (.)|.";
Try that:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
saveFileDialog1.Filter = filter;
const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
StreamWriter writer = null;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
writer.WriteLine(header);
writer.Close();
}
You use the SavefileDialog property "Filter" to define your list to filter by.

Winforms Save as

Does anyone know any articles or sites showing how to create a "Save as" dialogue box in win forms. I have a button, user clicks and serializes some data, the user than specifies where they want it saved using this Save as box.
You mean like SaveFileDialog?
From the MSDN sample, slightly amended:
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
dialog.FilterIndex = 2 ;
dialog.RestoreDirectory = true ;
if (dialog.ShowDialog() == DialogResult.OK)
{
// Can use dialog.FileName
using (Stream stream = dialog.OpenFile())
{
// Save data
}
}
}
Use the SaveFileDialog control/class.
Im doing a notepad application in c# i came over this scenario to save file as try this out.It will work perfectly
private void saveAsToolStripMenuItem_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)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName.ToString());
file.WriteLine(richTextBox1.Text);
file.Close();
}
}

Categories

Resources