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);
}
Related
i'm developing an HTML editor in C# with the FastColoredTextBox.dll component. I have the code for the "Save As" option. But when the file is saved, and i tried pressing the Save As option again, the SaveFileDialog will come up. I wanted it to save to the file that we saved earlier. Here's the Save As code:
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
if (FastColoredTextBox1.Text.Length > 0)
{
sfd = new SaveFileDialog();
sfd.Filter = "HTML Files|.html|" + "All Files|*.*";
sfd.DefaultExt = "html";
sfd.ShowDialog();
string location = null;
string sourcecode = FastColoredTextBox1.Text;
location = sfd.FileName;
if (!object.ReferenceEquals(sfd.FileName, ""))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(location, false))
{
writer.Write(sourcecode);
writer.Dispose();
this.Text = "Netplait 2.5.1 - " + System.IO.Path.GetFullPath(location);
}
}
}
if (Directory.Exists(sfd.FileName) == true)
{
string location = sfd.InitialDirectory;
File.WriteAllText(location, (FastColoredTextBox1.Text));
}
}
Please help.
I must say, this code seems a bit chaotic to me.
Why not just create the dialog instead?
SaveFileDialog sfd = default(SaveFileDialog);
And this, why not just check sfd.FileName != "" or better !string.IsNullOrWhitespace(sfd.FileName)
if (!object.ReferenceEquals(sfd.FileName, ""))
Am I mistaken, that sfd.FileName should return a file name, not a directory?
Because then this code does not make sense:
if (Directory.Exists(sfd.FileName) == true)
Anyway, before opening the dialog, you can set sfd.InitialDirectory and sfd.FileName as per documentation
SaveFileDialog
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");
}
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();
}
}