I'm trying to write to a text file, and I'm using the example from MSDN but I can't figure out how to add text to it?
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();
}
}
}
I've changed // Code to write the stream goes here. to a couple of things, like:
string thing = "This gets written";
and
"This gets written";
But that didn't work and I have no other ideas :L
using (var myStream = saveFileDialog1.OpenFile())
using (var writer = new StreamWriter(myStream))
{
writer.WriteLine("File content goes here");
}
Related
I created a new project in Windows Forms App (.NET Framework), this is the code I have:
private void SaveProxyResults_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
dlg.Dispose();
}
With this current code the save file menu pops up but it doesn't automatically go to your Desktop and there's no file type chosen automatically, I also can't save it as .txt because it gives me an error.
How do I have to edit the code in order to make it automatically choose .txt as file format, be able to type in the file name and automatically select your Desktop as file saving location while still being able to change the location of where the file is supposed to be saved?
Stream myStream;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
if ((myStream = dlg.OpenFile()) != null)
{
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
dlg.Dispose();
}
Gives me the error:
System.IO.IOException: 'The process cannot access the file 'C:\Users\JP\Desktop\Scraped Proxies123' because it is being used by another process.'
According to your description, you want it to automatically select .txt as the file format
and be able to type the file name and automatically select the desktop as the file saving
location.
You can try the following code to solve this problem.
Stream myStream;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "";
dlg.InitialDirectory = #"C:\Users\username\Desktop";// Use the absolute path of your computer desktop
dlg.Filter = "txt files (*.txt)|*.txt";
dlg.FilterIndex = 1;
if (dlg.ShowDialog() == DialogResult.OK)
{
if ((myStream = dlg.OpenFile()) != null)
{
myStream.Close();
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
}
dlg.Dispose();
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");
}
I am using LinqToCSV to export list of values in C#, and it works fine when I use only two lines as
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, "C://FileName.CSV");
But I want to use SaveFileDialog to allow user to choose the location where he wants to save the file. I did the following for this purpose. I took this code from here
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "csv files (*.csv)|*.csv";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, saveFileDialog1.FileName);
myStream.Close();
}
}
It gives an error "The process cannot access the file 'C:\aaa' because it is being used by another process" on line CSVContext.Write(bullishRowList, saveFileDialog1.FileName); I can't figure out what is the problem with this piece of code.
please help me.
It is your own process that opens the file in this line
if ((myStream = saveFileDialog1.OpenFile()) != null)
and that line is not necessary
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName != string.Empty)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, saveFileDialog1.FileName);
}
}
I have never used that library, but looking briefly at their documentation, if you want to open yourself the stream, then you need to pass the opened stream to the Write method
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, myStream);
myStream.Close();
}
}
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.
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();
}
}