save as dialog to save TextBox Data into a file - c#

I have one TextBox, It has some data.
There is a Button. So When I click the button, "save as " dialog should popup to save the text TextBox Data into a file.
I have tried various ways, but getting errors n error. Here I am giving you brief idea how I am writing code, If I am wrong please make me correct.
Or is there any other way to save TextBox Data into a file od my desired path.
protected void ButtonIDSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
fs.Close();
}
}
Thanks
Vivek

SaveDialog.OpenFile creates a new file (overwriting an existing file with the same choosen name) and returns a Stream object that could be used as the constructor parameter for a StreamWriter.
So you could simply write
if (saveFileDialog1.FileName != "")
{
using(StreamWriter sw = new StreamWriter(saveFileDialog1.OpenFile()))
{
sw.Write(TextBox1.Text);
}
}

Related

How to overwrite an existing html file from textbox in C#?

I'm trying to create a normal HTML editor where it's functions are similar to Windows Notepad. Let's say we've written a file and wanted to save it. So the SaveFileDialog comes up and asking us where we want to save it. Then we modified it, and in Notepad you usually just use Ctrl+S to save it again. And this time, no SaveFileDialog will show up. In other words, the existing file has been overwritten. This is what I wanted to achieve. Here's the code I have so far:
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
string location = null;
string sourcecode = textBox1.Text;
sfd = new SaveFileDialog();
location = sfd.FileName;
if (string.IsNullOrEmpty(sfd.FileName))
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
File.Copy(sfd.FileName, Path.Combine(location, Path.GetFileName(sfd.FileName)), true);
}
}

Open/save multiple textbox values c# windows forms

I created simple example and I can't find solution anywhere. I want to save UI textbox data, and open it in another(same) windows form file.
Here is code example:
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter write = new StreamWriter(File.Create(sfd.FileName));
write.Write(txtFirstInput.Text);
write.Write(txtSecondInput.Text);
write.Close();
write.Dispose();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
txtFirstInput.Text = read.ReadLine();
txtSecondInput.Text = read.ReadLine();
read.Close();
read.Dispose();
}
}
The problem I get here is that all inputed data get in first textbox. How to separate it? What is the easiest and most efficient way?
Should I create and use byte buffer (and HxD) for streaming through user input?
Can someone please give me some directions or examples.
Thank you for your time
You use ReadLine to read data back, so you need to use WriteLine when writing data to disk
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using(StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
{
write.WriteLine(txtFirstInput.Text);
write.WriteLine(txtSecondInput.Text);
}
}
}
Using Write, the data is written to disk without a line carriage return and so it is just on one single line. When you call ReadLine all the data is read and put on the first textbox.
Also notice that it is always recommended to enclose a IDisposable object like the StreamWriter inside a using statement. This approach will ensure that your Stream is correctly closed and disposed also in case of exceptions. (The same should be applied to the StreamReader below)

Adding a Save As Function to a form using a listbox store of data

I am creating a Times table form in Visual Studio Express 2014 in C#.
I have created the whole form, it works to a full extent, even saving to a text file, however i wish to add a Savedialog in order for the user to choose where to save the file just like saving in word, etc.
Ive tried using File.WriteAllText(name, "test"); or variations of this however this does not work with a listbox
In my code the listbox is called results,
here is my save button code what i have tried so far:
private void save_Click(object sender, EventArgs e)
{
const string Path = "C:\\Users\\Loan\\save.txt";
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(Path);
foreach (var item in results.Items)
{
SaveFile.WriteLine(item.ToString());
}
SaveFile.Close();
MessageBox.Show("Programs saved!");
}
it works perfectly fine, however as i have aforementioned that
i wish to create a save as function for the user to browse where to
save.
The form =
http://gyazo.com/227d2aada349586cef60f2962456a71c
The full code =
http://pastebin.com/7DwLpkhP
Use the `Save file dialog' class
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
string Path = dialog.FileName;
....

Writing PictureBox image to disc via dialog

I was wondering if someone can help me troubleshoot.
I'm new to programming and was looking to make my own photo viewer. I have it working great for the most part.
My issue is I wanted to be able to save the image I open into the viewer as a new image. I have some buttons to make changes to the picture. My problem is I have a save dialog that when done defining the save, and you press save, it just re-opens a new save dialog box and doesn't write to disc. My question is how can I actually write to file and prevent the save as showing up everytime I attempt to save using the dialog box?
Here is my code:
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//Displays a SaveFile Dialog so the user can save the image
//assigned to SaveAs tool strip
SaveFileDialog savefiledialog1 = new SaveFileDialog();
savefiledialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|Png Image|*.png";
savefiledialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
//if the file name is not an empty string open it for saving.
if (savefiledialog1.FileName != "")
{
//Saves the image via filestram created by Openfile method.
System.IO.FileStream fs =
(System.IO.FileStream)savefiledialog1.OpenFile();
//Saves the image in the appropriate Imageformat based upon the
//file type selected in the dialog box.
//NOTE that the filterIndex property is one-based.
switch(savefiledialog1.FilterIndex)
{
case 1 :
this.saveAsToolStripMenuItem.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
this.saveAsToolStripMenuItem.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
this.saveAsToolStripMenuItem.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
case 4:
this.saveAsToolStripMenuItem.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Png);
break;
}
fs.Close();
}
}
some where in your code you should have something like
partial class MainWindow : Window
{
List<someImageClass> images;// <------this here
/*someImageClass should store the paths to the images */
string currentimage; //maintain this or if you are binding to listbox or other list control
//you will be able to use ((someImageClass) listcontrol.selectedItem).path
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//so in here you can
if(!File.Exists(currentimage.path))
{
/*show dialog*/
**EDIT** missed save via dialog part
if(savelog.ShowDialog ?? false == true)
{
//depends on how you are storing image data
// see below
imageclassinstance.save(savelog.FileName);
}
}
else
{
using(FileStream fs = new FileStream(currentimage.path,FileMode.CreateNew,FileAccess.ReadWrite))
using(BinaryWriter bw = new BinaryWriter(bw))
{
bw.WriteByte();//loop this images are not 1 byte
/*you don't really need binarywriter it will depend on what you
are using to store the image data. some types have
built in save functions*/
}
}
.....
}
}

How to make a selected path to save an XML file?

I have a function that can save a .xml file.
private void buttonSaveXML_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "XML Files|*.xml";
saveFile.Title = "Save a Xml File";
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
FileStream fs = (FileStream)saveFile.OpenFile();
dsVersions.WriteXml(fs);
}
}
What do I add to make a specific path that I want to save it to?
You can control the InitialDirectory so that the user will be "in the right place", but you cannot prevent them from switching directories with SaveFileDialog.
That way, they will be in your default path rather than e.g. on the Desktop.
saveFile.InitialDirectory = #"C:\My\Path" ;
Typically I will save the last directory that the user selected to save files to in application configuration and use the user's last directory as the InitialDirectory.
Use the property InitialDirectory of SaveFileDialog form. For Example add this to your code:
saveFile.InitialDirectory = "C:\\MyXMLs\\";
You can see the Documentation.

Categories

Resources