Open File Dialog Initial directory - c#

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;

Related

How to set a Initial file name in a SaveFileDialog?

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

Getting the path of a file from the SaveFileDialog to a string

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

Create a “Open” Dialog Button and get the the file name with extension .dat

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.

File Copy in C#.Net

I'm trying to build a copier so that you use the openFileDialog to chose a file and then the folderBrowserDialog to choose the location to copy it to.
The problem I'm having is that when I use File.Copy(copyFrom,copyTo) it gives me an exception that I can't copy to a directory.
Is there anyway around this, or am I just missing something stupid and noobish. I've tryed useing openFD for choosing both locations and have just tried using the folderBD to see if it made a difference.
I know there should be if statements there to catch the exceptions but this is a rough draft of the code to get working 1st.
Thanks in advance for the help, code attached.
// Declare for use in all methods
public string copyFrom;
public string copyTo;
public string rootFolder = #"C:\Documents and Settings\cmolloy\My Documents";
private void btnCopyFrom_Click(object sender, EventArgs e)
{
// uses a openFileDialog, openFD, to chose the file to copy
copyFrom = "";
openFD.InitialDirectory = rootFolder;
openFD.FileName = "";
openFD.ShowDialog();
// sets copyFrom = to the file chosen from the openFD
copyFrom = openFD.FileName;
// shows it in a textbox
txtCopyFrom.Text = copyFrom;
}
private void btnCopyTo_Click(object sender, EventArgs e)
{
//uses folderBrowserDialog, folderBD, to chose the folder to copy to
copyTo = "";
this.folderBD.RootFolder = System.Environment.SpecialFolder.MyDocuments;
this.folderBD.ShowNewFolderButton = false;
folderBD.ShowDialog();
DialogResult result = this.folderBD.ShowDialog();
// sets copyTo = to the folder chosen from folderBD
copyTo = this.folderBD.SelectedPath;
//shows it in a textbox.
txtCopyTo.Text = copyTo;
}
private void btnCopy_Click(object sender, EventArgs e)
{
// copys file
File.Copy(copyFrom, copyTo);
MessageBox.Show("File Copied");
You have to append the file name to the directory path. Do this:
string destinationPath = Path.Combine(copyTo, Path.GetFileName(copyFrom));
File.Copy(copyFrom, destinationPath);
(with this you'll copy selected file to another directory with the same name of the original one and it'll throw an exception if the same file already exist in that directory)
Edit
Side note: do not hard code the path in your source code, use this:
rootFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
to get the path of current user's documents folder.
Do this:
File.Copy(copyFrom, Path.Combine(copyTo, Path.GetFileName(copyFrom)));
File.Copy needs to know the full path of the new file you want, including the file name. If you just want to use the same file name, you can use this to append the file name to the path:
copyTo = Path.Combine(copyTo, Path.GetFileName(copyFrom));

C# FilePath Help

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)

Categories

Resources