Search code examples
c#formsfilesystemwatcherfolderbrowserdialog

Can we choose a particular file using folderbrowser dialogue and then copy the path of that file with its name to a text box


This click event open the folderbrowser dialog.. but my customer wants to watch only a particular text file in the folder to be watched by filewatcher.. and that path of the file should be written to the text box in the form

private void BrowserBtn_Click(object sender, EventArgs e)
    {
        //string startingDir = this.txtBoxPath.Text;
        FolderBrowserDialog dlg = new FolderBrowserDialog();
        dlg.SelectedPath = this.txtBoxPath.Text;
        DialogResult result = dlg.ShowDialog();

        if (result == DialogResult.OK)
        {
            if (!string.IsNullOrEmpty(dlg.SelectedPath))
            {
                this.txtBoxPath.Text = dlg.SelectedPath;
            }
        }

        if (string.IsNullOrEmpty(this.txtBoxPath.Text))
        {
            this.txtBoxPath.Text = appDataFolder;
        }
        if (!Directory.Exists(path))
        {
            MessageBox.Show("Specified folder does not exist");
        }
    }

I serched trough internet.. but i couldnt find the way to select the files in the folder.. or should i use Openfiledialogue? doesnt open file dialogue opens the file? i dont wanna use it coz i dont want to open the file but just watch the changes in the selected file..


Solution

  • The files aren't open at this stage:

    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        if( DialogResult.OK == ofd.ShowDialog(this))
        {
            //Do something with the following files:
            //ofd.FileNames
        }
    }
    

    Or if you want to process a single file only you can use the FileName property. Files are not yet opened until you do something with those file(s).