12

In my case the SaveFileDialog will not write any file, but I want to use to specify the path for a command line app which will create the logfile on the same location as "saved" in the sf dialog.

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFileName(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFileName(sfd.FileName);
0

6 Answers 6

16

You can use

Path.GetFullPath(sfd.FileName);

Instead of

Path.GetFileName(sfd.FileName);

Complete version...

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFullPath(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFullPath(sfd.FileName);
6

Just remove the Path.GetFileName:

startInfo.Arguments = "--log=\"" + sfd.FileName + "\"";
2
  • With this I get the full path of the file (I checked with a textbox) but the command line app don't recognize it.
    – user830054
    Dec 7, 2011 at 14:50
  • @user830054: Try adding quotes around the path. See updated answer. Dec 8, 2011 at 13:16
2

I think you are using the wrong dialog form based on what you are describing.

Try using the FolderBrowserDialog class:

string folderPath = string.Empty;

using (FolderBrowserDialog fdb = new FolderBrowserDialog()) {
  if (fdb.ShowDialog() == DialogResult.OK ){
    folderPath = fdb.SelectedPath;
  }
}

if (folderPath != string.Empty) {
  startInfo.Arguments = "--log=" + folderPath;
}
2
OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image files | *.jpg";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                 employee_dp.Image = Image.FromFile(openFileDialog1.FileName);
                 string path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                 string onlyFileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
                 filepath = Path.GetFullPath(path).Replace(@"\", @"\\");
                 filepath = filepath + "\\\\" + onlyFileName;
                 MessageBox.Show(filepath);
2
0

Perhaps Path.GetFullPath would help?

0

The problem might be using the wrong FileSaveDialog.
The one in Win32.dll doesn't provide the full path, but the one in System.Windows.Forms does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.