2016-09-29 36 views
0

我基本上只是想獲得一個文件路徑來保存文件,但我的SaveFileObject不會讓我訪問SelectedPath。我檢查了其他論壇,無法弄清楚爲什麼它贏得了'tlet我,這是我的代碼;在C#Winforms中使用SaveFileDialog

SaveFileDialog filePath = new SaveFileDialog(); 
DialogResult result = filePath.ShowDialog(); 


    if (result == DialogResult.OK) 
    { 
     string folderPath = filePath.; 
    } 

它會讓我再次選擇filePath.ShowDialog和filePath.ToString等...我哪裏錯了?

+1

你是說這不會讓你做folderPath = filePath.FileName; – dazedandconfused

+1

當你發現你的猜測是錯誤的,並且選擇不閱讀文檔以找出該屬性的名稱時,你出錯了。 –

+0

它不會讓我寫「string folderPath = filePath.SelectedPath」來獲取用戶選擇的文件路徑後filePath.ShowDialog() –

回答

1

實際上您需要SaveFileDialog中FileName屬性的文件名。這將爲您提供用戶想要保存的文件的完整路徑和文件名。

SaveFileDialog saveDialog = new SaveFileDialog(); 
DialogResult result = saveDialog.ShowDialog(); 
if (result == DialogResult.OK) 
{ 
    String fileName = saveDialog.FileName; 
    //your code to save the file; 
} 

雖然自從.ShowModal()返回的DialogResult,你可以直接在使用它,如果以備用的一行代碼(男人我是貪婪)