How to make a selected path to save an XML file? - c#

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.

Related

If initial directory doesn't exist, create it, but if user cancel the save, delete the newly added folders

I want to save a textbox text to a .txt file created in a special directory, using a SaveFileDialog.
But let's say my path doesn't exist: I would like that when the dialogbox shows up to ask where the user want to save his .txt file, the dialogbox create automatically the missing folders if they are missing. But I also like that if the user cancel his saving, the newly created folder to erase if they are empty.
In other words: the SaveFileDialog dialogbox shows up in an initial directory, but if this initial directory is null, my code generate this directory BUT if the user cancel, my code erase the generated directory.
Here's my example: I want to save my .txt in Desktop\FolderExistingOrNot, but if the folder FolderExistingOrNot I want to creat it. But if the user cancels, I want to delete if FolderExistingOrNot is empty.
private void btn_SAVE_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\FolderExistingOrNot";
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
// I would like to erase the folder FolderExistingOrNot if it's empty
}
}
It might be simple but I haven't figured out how to do it.
This worked for me when I tested it.
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\FolderExistingOrNot";
Directory.CreateDirectory(mypath);
sfd.InitialDirectory = mypath;
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
if (Directory.GetFiles(mypath).Length == 0)
{
Directory.Delete(mypath);
}
}

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);
}
}

Can we choose a particular file using folderbrowser dialogue and then copy the path of that file with its name to a text box

This click event open the folderbrowser dialog.. but my customer wants to watch only a particular text file in the folder to be watched by filewatcher.. and that path of the file should be written to the text box in the form
private void BrowserBtn_Click(object sender, EventArgs e)
{
//string startingDir = this.txtBoxPath.Text;
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = this.txtBoxPath.Text;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
if (!string.IsNullOrEmpty(dlg.SelectedPath))
{
this.txtBoxPath.Text = dlg.SelectedPath;
}
}
if (string.IsNullOrEmpty(this.txtBoxPath.Text))
{
this.txtBoxPath.Text = appDataFolder;
}
if (!Directory.Exists(path))
{
MessageBox.Show("Specified folder does not exist");
}
}
I serched trough internet.. but i couldnt find the way to select the files in the folder.. or should i use Openfiledialogue? doesnt open file dialogue opens the file? i dont wanna use it coz i dont want to open the file but just watch the changes in the selected file..
The files aren't open at this stage:
using (OpenFileDialog ofd = new OpenFileDialog())
{
if( DialogResult.OK == ofd.ShowDialog(this))
{
//Do something with the following files:
//ofd.FileNames
}
}
Or if you want to process a single file only you can use the FileName property. Files are not yet opened until you do something with those file(s).

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;
....

How to set default path, then I press ShowDialog button in WPF?

So currently I get user decided path to file this way:
private void Button_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".xml";
dlg.Filter = "XML files (*.xml)|*.xml";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string xmlFile = dlg.FileName; // this required full path.
}
}
It works fine, but usually default path is .exe locating folder and need to change it. How can I do it?
If you are wanting to have the dialog open to a specific directory when .ShowDialog() is called you can set the InitialDirectory property to whatever path pleases you.
When you do this it's good practice to set the OpenFileDialog to null when you are finished.

Categories

Resources