private void SaveFile()
{
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)
{
rtb1.Text().get();
}
}
}
I use this when trying to get the text of a richtextbox and save it but it always comes up with error, CS1955
If you just want to write a simple text file then you can use the File.WriteAllText command instead.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.Filename, rtb1.Text);
}
You just need to specify the filename with path, and then the text, which in this case is rtb1.Text (no brackets!)
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 have looked around on several answers to similar questions, but somehow this isnt working for me.
I am trying to save the contents of a textbox into a user promptet file.
private void btnSave_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)
{
File.WriteAllText(saveFileDialog1.FileName, rtbIncoming.Text);
myStream.Close();
}
}
}
The User prompt pops up as expected, and the file is generated but without any content.
You don't need to open the file stream yourself. File.WriteAllText() does all this for you. So this should be enough:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
File.WriteAllText(saveFileDialog1.FileName, rtbIncoming.Text);
I guess your code leads to an empty file because you open a seperate stream that isn't used to write and closed (and flushed) after the call to WriteAllText().
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();
}
}
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();
}
}