0

I see strange behavior of SaveFileDialog. I expect that FileName property contains full path to new file. And I don't have idea how to know where to save file. Dialog object dump follows (after user selected file):

_autoUpgradeEnabled true    bool
_customPlaces   Count = 0   System.Windows.Forms.FileDialogCustomPlacesCollection
AddExtension    true    bool
AutoUpgradeEnabled  true    bool
charBuffer  null    System.Windows.Forms.UnsafeNativeMethods.CharBuffer
CheckFileExists false   bool
CheckPathExists true    bool
CustomPlaces    Count = 0   System.Windows.Forms.FileDialogCustomPlacesCollection
DefaultExt  ""  string
defaultExt  null    string
DereferenceLinks    true    bool
DialogCaption   ""  string
dialogHWnd  0   System.IntPtr
FileName    "temp.flg"  string
fileNames   {string[1]} string[]
FileNames   {string[1]} string[]
FileNamesInternal   {string[1]} string[]
Filter  "TIFF (*.tif)|*.tif|Multipage image (*.tif)|*.tif|GIF (*.gif)|*.gif|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp|VASoftOnline Flow Chart (*.flg)|*.flg"    string
filter  "TIFF (*.tif)|*.tif|Multipage image (*.tif)|*.tif|GIF (*.gif)|*.gif|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp|VASoftOnline Flow Chart (*.flg)|*.flg"    string
FilterExtensions    {string[1]} string[]
filterIndex 7   int
FilterIndex 7   int
FilterItems {System.Windows.Forms.FileDialogNative.COMDLG_FILTERSPEC[7]}    System.Windows.Forms.FileDialogNative.COMDLG_FILTERSPEC[]
ignoreSecondFileOkNotification  false   bool
initialDir  null    string
InitialDirectory    ""  string
Instance    16449536    System.IntPtr
okNotificationCount 0   int
Options 2052    int
options -2147481594 int
RestoreDirectory    false   bool
securityCheckFileNames  false   bool
SettingsSupportVistaDialog  true    bool
ShowHelp    false   bool
supportMultiDottedExtensions    false   bool
SupportMultiDottedExtensions    false   bool
title   null    string
Title   ""  string
UseVistaDialogInternal  true    bool
ValidateNames   true    bool
CreatePrompt    false   bool
OverwritePrompt true    bool

You can see in dump that FileName = "temp.flg" without any information about path. Do anyone saw that problem in his software? Are there ideas how to fix it?

Vitalii
  • 4,434
  • 4
  • 35
  • 77

3 Answers3

1

In the documentation of SaveFileDialog you can find an example how to save file using stream:

Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
   if((myStream = saveFileDialog1.OpenFile()) != null)
   {
       // Code to write the stream goes here.
       myStream.Close();
   }
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
0

FileName will contain the file name of the file. If you want the file path you could use this:

FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
fileInfo.DirectoryName //Full path to file
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
dtsg
  • 4,408
  • 4
  • 30
  • 40
  • in my experience (and according to the documentation) that property should include the path. Testing this locally right now, I get a full filepath from that property. If the OP is really just getting a filename (and not a relative path, with is what I suspect) then your code will get the path of *the working directory of the application*, not in the folder that the user specified. – Dan Puzey Aug 14 '12 at 08:26
-1

The filename that the dialog returns should be a full filename including the path. Perhaps you're getting a relative path instead of an absolute path back, but I'm not sure why this would be the case. Locally I've tested this and I see the full path each time.

However, there's something else that may help: the SaveFileDialog class has a method called OpenFile that you can use to create the file and return a Stream for you to to write to:

var dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
    using (var stream = dlg.OpenFile())
    {
        // save here!
    }
}
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96