How to get the directory path

0

I'm using WinForm and trying to save a string with SaveFileDialog but I can not pass the chosen directory in SaveFileDialog to the System.IO.File.WriteAllLines;

        private void saveCategoriesToolStripMenuItem_Click(object sender, EventArgs e)
    {

        string[] SaveString = new string[100];
        SaveFileDialog SFD = new SaveFileDialog();
        SFD.Filter = "Categories Files (.ctg)|*.ctg";
        SFD.Title = "Save a Categories file";
        SFD.ShowDialog();

         if (SFD.FileName == "")
        {
            MessageBox.Show("Enter a name for the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            SaveString[0] = tamanho.ToString();
            for(int i=1;i<=tamanho;i++)
            {
                SaveString[i] = "\"" + KeysValues[i] + "\",\"" + CategoriesValues[i] + "\"";
            }
            SFD.DefaultExt = ".ctg";
            System.IO.File.WriteAllLines(, SaveString);
        }
    
asked by anonymous 02.08.2016 / 05:34

1 answer

1

Caio, what you are looking for is the class FileInfo , and can be used as follows

FileInfo fileInfo = new FileInfo(SFD.FileName);

System.IO.File.WriteAllLines(fileInfo.DirectoryName, SaveString);

In this case, fileInfo.DirectoryName returns the directory of the file selected by SaveFileDialog

    
02.08.2016 / 13:24