首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Windows.Forms.SaveFileDialog将FileName设置为不带路径的文件标题。为什么?

System.Windows.Forms.SaveFileDialog将FileName设置为不带路径的文件标题。为什么?
EN

Stack Overflow用户
提问于 2012-08-14 16:07:23
回答 3查看 2.1K关注 0票数 0

我看到了SaveFileDialog的奇怪行为。我希望FileName属性包含新文件的完整路径。我不知道如何知道在哪里保存文件。对话对象转储随后(在用户选择的文件之后):

代码语言:javascript
复制
_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

您可以在转储中看到FileName = "temp.flg“,但没有任何有关路径的信息。有没有人在他的软件中发现了这个问题?有没有办法解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-14 16:20:42

documentation of SaveFileDialog中,您可以找到如何使用stream保存文件的示例:

代码语言:javascript
复制
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();
   }
}
票数 1
EN

Stack Overflow用户

发布于 2012-08-14 16:14:03

FileName将包含文件的文件名。如果需要文件路径,可以使用以下命令:

代码语言:javascript
复制
FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
fileInfo.DirectoryName //Full path to file
票数 0
EN

Stack Overflow用户

发布于 2012-08-14 16:24:03

对话框返回的文件名应该是包含路径的完整文件名。也许你得到的是相对路径,而不是绝对路径,但我不确定为什么会出现这种情况。我已经在本地测试过了,每次都会看到完整的路径。

但是,还有一些其他方法可能会对您有所帮助:SaveFileDialog类有一个名为OpenFile的方法,您可以使用该方法创建文件并返回一个Stream供您写入:

代码语言:javascript
复制
var dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
    using (var stream = dlg.OpenFile())
    {
        // save here!
    }
}
票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11948203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档