I created a script for a save-file-dialog to save a file. It has the Initial location set to the desktop. Now my question is, how do i set an initial "name" for the file in the dialog?
Here's my code:
private SaveFileDialog save = new SaveFileDialog();
private void Information(string Basic, string nameoffile, string program)
{
if (doingsomething) return;
System.Windows.Forms.MessageBox.Show($"Please select where you would like to store the file)"
save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (save.ShowDialog() == DialogResult.OK)
If there is an answer for this, please point where I can find it. Because i don't know the exact keyword.
You can asssign default name like
save.FileName="File1";
Related
EDIT: The main issue when renaming the file is to rename xaml contents
My intuition is screaming that there is a better way to do this, but the following works, my question is to see if there a better or an already existing method that can do this
I have a small WPF app that loads xaml files and can be imported/edited and exported.
When the WPF app initialises it makes a placeholder xaml file that you can edit,
"StartingXamlFile.xaml" when exporting I used a SaveFileDialog and the user can change the name. But the contents of the XAML file is not changed according to the file name. Also when importing an existing XAMLfile then editing and exporting(changing name when exporting) the same thing occurs.
Solution:
I set up a Singleton object to hold the file name XamlFile with property content and path. Now the default is set to "StartingXamlFile" when importing this is replaced. This is done following Gang of four Singleton technique
SaveFile code that doesnt work:
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "StartingXamlFile"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "xaml files (*.xaml)|*.xaml|All files (*.*)|*.*"; // Filter files by extension
// Show save file dialog box
var result = dlg.ShowDialog();
if (result == true)
{
File.WriteAllText(dlg.FileName, editedContent);
}
Save File code that does work using Replace:
if (result == true)
{
var name = Path.GetFileNameWithoutExtension(dlg.FileName);
var testing = XamlFile.Content.Replace(XamlFile.Path,name);
File.WriteAllText(dlg.FileName, testing);
}
Just to give this question an accepted answer, I have not found a better way to this other than:
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "StartingXamlFile"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "xaml files (*.xaml)|*.xaml|All files (*.*)|*.*";
// Show save file dialog box
var result = dlg.ShowDialog();
if (result == true)
{
//X:Class replacement is done here
var name = Path.GetFileNameWithoutExtension(dlg.FileName);
var testing = XamlFile.Content.Replace(XamlFile.Path,name);
File.WriteAllText(dlg.FileName, testing);
}
XamlFile is a singleton object that contains the content of the xaml
and initial name of file when starting up or importing, so when you export it
replaces all references to the initial name
I have problem with InitialDirectory path i used part of code shown below. OpenDialog always show directory where i open file last time but i couldn't set new relative path.. I tried set absolute path but it didn't work also.
private static string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
public static string OpenDialog()
{
// Create OpenDialog
var dlg = new Microsoft.Win32.OpenFileDialog();
// initial directory for OpenFileDialog need fix
if(Directory.Exists(path))
{
dlg.InitialDirectory = path;
}
dlg.RestoreDirectory = true;
In your example, 'path' is being set to your .exe, which will cause if (Directory.Exists(path)) to fail, therefore, the dialog will open to the last known good directory, because InitialDirectory will not be set to the value that you want. Try simply hard-coding a known good directory path first. Or you could do something like this to fix it:
path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
In my form I have a button that launches the SaveFileDialog module. Then when I load a file, I want to save the path as a string and put that text into a text box on the form. I'm not sure how to do this, or even where to start?
Well the problem with your question is that you say when you "load a file", but you cannot load a file from the SaveFileDialog module. However, if you are opening a file via the OpenFileDialog module, then you are able to use this solution to get the directory path of the file you just loaded:
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
var directoryPath = Path.GetDirectoryName(openFileDialog1.FileName);
if(!string.IsNullOrEmpty(directoryPath))
textBox1.Text = directoryPath;
}
Otherwise, if you are wanting to get the file path of whatever file you saved originally, you can use pretty much the same solution to get the directory path:
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
var directoryPath = Path.GetDirectoryName(saveFileDialog1.FileName);
if (!string.IsNullOrEmpty(directoryPath))
textBox1.Text = directoryPath;
}
I want to create a browse (OpenFile Dialog) Button to search my local drive and then write out the selected file name (not the full path ) to a TextBox. It should show Only .dat extension files.
I am using Visual Studio 2008
Any help much appreciated!
Next time you ask anything, show some examples of what you have tried please.
private string GetDatFileName()
{
// Create Open File Dialog with the correct filter
using (OpenFileDialog ofd = new OpenFileDialog()) {
ofd.Filter = "dat-file (*.dat) | *.dat";
string fileNameAndFolder = "";
string fileName = "";
// Get file plus folder.
if (ofd.ShowDialog() == DialogResult.OK)
{
fileNameAndFolder = ofd.FileName;
// Split folder and filename
fileName = Path.GetFileName(fileNameAndFolder);
}
// Return the fileName;
return fileName;
}
}
What I have done here is create an OpenFileDialog and set its filter to the required "dat"-format. Only .dat-files will show up in the browserdialog.
Next, you show the dialog and check if the result is OK. If the result is, you will get the full filename (with folder) into a variable. All thats left then, is to get the filename from fileNameAndFolder.
I use OpenFileDialog to search for a specific file. When the user chooses the file, I want to store that path in a variable. However, these doesn't seem to be an option for this within OpenFileDialog?
Does anybody know how to do this?
Thanks.
Edit: This is Winforms, and I don't want to save the path inclusive of the filename, just the location where the file is.
If you are using WinForms, use the FileName property of your OpenFileDialog instance.
On WinForms:
String fileName;
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Ok) {
fileName = ofd.FileName;
}
//getting only the path:
String path = fileName.Substring(0, fileName.LastIndexOf('\\'));
//or easier (thanks to Aaron)
String path = System.IO.Path.GetDirectoryName(fileName);
This will retrieve your path based on the FileName property of the OpenFileDialog.
String path = System.IO.Path.GetDirectoryName(OpenFileDialog.FileName);
Instead of copy pasting answers from MSDN I'll just link to them.
MSDN documentation on Forms OpenFileDialog.
MSDN documentaiton on WPF OpenFileDialog.
Please try to look for a answer before posting questions.
You store the path ... somewhere else!
What I usually do is create a user-scoped configuration variable.
Here's a sample of its use:
var filename = Properties.Settings.Default.LastDocument;
var sfd = new Microsoft.Win32.SaveFileDialog();
sfd.FileName = filename;
/* configure SFD */
var result = sfd.ShowDialog() ?? false;
if (!result)
return;
/* save stuff here */
Properties.Settings.Default.LastDocument = filename;
Properties.Settings.Default.Save();
To save just the directory, use System.IO.Path.GetDirectoryName()
After the dialog closes there should be a file path (or something similar) property on the OpenFileDialog object, it will store whatever file path the user entered.
Try FileName. Or FileNames if you allow multiple files to be selected(Multiselect=true)