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 ChandPosted Jul 27, 2010, 10:08 AM
http://www.c-sharpcorner.com/UploadFile/mahesh/1878/
CherRon McLemorePosted Jul 27, 2010, 11:30 AM
fileName = Path.GetFileName(save.FileName);
Thanks for posting guys!!! :)
Hirendra SisodiyaPosted Jul 27, 2010, 10:07 AM
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 McLemorePosted Jul 27, 2010, 9:33 AM
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?
Mahesh ChandPosted Jul 26, 2010, 9:54 PM