Hello. I am working on a GUI that will write the contents of a listbox to a text file. After writing the last item, a saveFileDialog pops up. Then you can type in the name of the file and continue to save it to the desired location. My question is how can I access the name of the file I type in to save it to a variable?
Ex. I type in "testerFile" to save as the filename. How do I access that from the dialog textbox to save it to a global string?
Loading

Mahesh ChandRank #1 · 393.3K reputationPosted Jul 27, 2010, 6:08 AM EDT
http://www.c-sharpcorner.com/UploadFile/mahesh/1878/
Mahesh ChandRank #1 · 393.3K reputationPosted Jul 26, 2010, 5:54 PM EDT
CherRon McLemore32 reputationPosted Jul 27, 2010, 5:33 AM EDT
Ex.
//Global string variable
string fileName;
private static void saveImage()
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = Directory.GetCurrentDirectory();
save.Filter = "txt files (*.txt)|*.txt";
if(save.ShowDialog() == DialogResult.OK)
{
fileName = (whatever I typed into the SaveFileDialog box);
}
}
I guess my question is what is the missing code to access what I typed into the savefiledialog box?
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Jul 27, 2010, 6:07 AM EDT
private static void saveImage()
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = Directory.GetCurrentDirectory();
save.Filter = "txt files (*.txt)|*.txt";
if (save.ShowDialog() == DialogResult.OK)
{
string TempfileName = save.FileName; //(whatever I typed into the SaveFileDialog box);
int Index1 = TempfileName.LastIndexOf("\\");
string fileName = TempfileName.Substring(Index1+2, (TempfileName.Length - Index1)-6);
}
}
thanks
please mark as answer if it helps
CherRon McLemore32 reputationPosted Jul 27, 2010, 7:30 AM EDT
fileName = Path.GetFileName(save.FileName);
Thanks for posting guys!!! :)