简体   繁体   中英

How do you get the inputted file name in savefiledialog?

If I were to enter a name for my file to save in the savefile dialog, how do I programmatically get the name I inputted? If I do saveFileDialog.fileName i'll get the full name. Say I entered "myfile" and it was saved in the folder "mynotepad". I want to get that "myfile" not "c:\\programfiles\\documents\\mynotepad\\myfile.txt".

Well, If you do know how to get c:\\programfiles\\documents\\mynotepad\\myfile.txt (and it appears like you do) than you can use the System.IO.Path class to get the filename

using System.IO; 

...

    string fn = Path.GetFileName(@"c:\programfiles\documents\mynotepad\myfile.txt");

if you don't want the .txt than use GetFileNameWithoutExtension

string fn = Path.GetFileNameWithoutExtension(@"c:\programfiles\documents\mynotepad\myfile.txt");

您可以使用Path.GetFileName()

string fileName = Path.GetFileName(saveFileDialog.FileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM