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);
LarsTech
  • 80,625
  • 14
  • 153
  • 225
user830054
  • 299
  • 2
  • 5
  • 14

6 Answers6

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 + "\"";
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
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

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;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
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.

Noich
  • 14,631
  • 15
  • 62
  • 90
0

Perhaps Path.GetFullPath would help?

oleksii
  • 35,458
  • 16
  • 93
  • 163