How to take the path of the directory

Asked

Viewed 639 times

0

I’m using Winform and trying to save a string with Savefiledialog but I can’t pass the chosen directory in Savefiledialog to the System.IO File function.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);
        }

1 answer

1


Caio, what you seek is class Fileinfo, can be used as follows

FileInfo fileInfo = new FileInfo(SFD.FileName);

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.