Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# FolderBrowserDialog problem

  • 23-05-2006 8:55am
    #1
    Closed Accounts Posts: 85 ✭✭


    Hi
    Has anyone dealt with this windows control ?

    I'm trying to get it to work so that the operator can select ANY folder, but it looks like you have to set a RootFolder property to one of several
    enumerated options, and if the folder that the user selects is not a subfolder of the chosen RootFolder, then tough luck.
    And of course none of the enumerated options for the rootfolder property includes the filesystem root ....................

    Surely there's a way of selecting any folder - if not, thats just shoddy on Microsofts part ................
    I've been fighting with it for three days and getting nowhere - has anyone else dealt with this ?


    Thanks
    Colm


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Can you post some code? I'm able to select any folder except My Computer (which isn't really a folder for reasons I've just to understand) using .Net 1.1 so I suspect I'm not understanding what you're really asking.


  • Closed Accounts Posts: 85 ✭✭protos


    I'll post the code tomorrow when I get into work.

    Basically though, what I'm trying to do, is create the dialog box and then
    write the selected directory path to file, so once the user clicks OK, another
    method is called which tries to write the selectedPath to file, but unless the
    selectedPath is a subfolder of what was set up as a rootFolder it won't work.

    Cheers
    Colm


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    I think the problem is setting a rootfolder in the first place. You don't have to do it to use the dialog.


  • Closed Accounts Posts: 85 ✭✭protos


    Thanks for your replies.

    The code looks like this :
    private void BrowseBtn2_Click(object sender, System.EventArgs e)
    {
    	FolderBrowserDialog fbd2 = new FolderBrowserDialog();
    	fbd2.ShowNewFolderButton = false;
            // fbd2.RootFolder = Environment.SpecialFolder.MyComputer;
    	// fbd2.SelectedPath = "C:\\";
    						
    	DialogResult dr = fbd2.ShowDialog();
    	if(dr == DialogResult.OK)		
    	{
    		userDirText2.Text = fbd2.SelectedPath;
    		this.writeFile("dialog2.txt", fbd2.SelectedPath);
    	}
    }
    
    
    
    
    private bool writeFile(string filename, string str)
    {
    	StreamWriter sw;
    	try
    	{		
    		progressBox.AppendText("Writing // " + filename + " : " + str + "\n");
    		sw = new StreamWriter(filename);
    		sw.Write(str);
    		sw.Flush();
    		sw.Close();
    		return true;
    	}
    	catch (System.IO.IOException) 
    	{
    		progressBox.AppendText("IO Error Writing To File.\n");
    		return false;
    	}
    }
    
    



    This brings up a dialog box with the rootFolder at the default (the desktop) and if I select a folder which exists on the desktop (is a desktop subfolder) then it works fine.

    If I browse to a folder on the c:\ drive though, it will display the path with no problems in a messageBox, but it will not write it to file.
    The file is never created and no exception is thrown.

    Also if I set the rootFolder to "my computer" or the selectedPath to an absolute path - as shown above in the commented out lines - it will show up alright in the dialog box, but again will not even try to write anything to file, once the OK button is pushed.

    Any ideas ? If I don't get this working soon I'll have to dump C# and move to java or something .......................

    Thanks
    Colm


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    You're using the streamwriter wrong. Thats all. try something like this...
    string temp = str + filename;  // look at this string and make sure it's a valid path when you're debugging
    StreamWriter writer = new StreamWriter(temp);
    

    Or as a more basic test...
    StreamWriter writer = new StreamWriter("c:\\testfile.txt");
    

    Also, go to the "debug" folder of your application (probably in bin/debug) and you should see a text file there with the name "dialog2.txt". And if you open that file, it should contain the path of the folder you selected.

    What you did was create a file called "dialog2.txt" (and because there is no path specified, it gets created in the same folder as your .exe) and you wrote the name of the selected directory INTO that file.


  • Advertisement
  • Closed Accounts Posts: 85 ✭✭protos


    What you did was create a file called "dialog2.txt" (and because there is no path specified, it gets created in the same folder as your .exe) and you wrote the name of the selected directory INTO that file.

    Actually thats what I'm trying to do !
    The app basically purges, temporary directories and cached files, but it also gives the user the option to select two directories of their own that they might want to purged. The idea is that the user selects the directory using the FolderBrowserDialog, and the selectedPath is then written to a preference file - "dialog2.txt" in this instance, but it doesn't work.

    I'm pretty sure the issue is with how the FolderBrowserDialog works - not with the IO code.
    Hopefully what I'm trying to do is clear now !! Sorry for the confusion.


  • Closed Accounts Posts: 85 ✭✭protos


    Bah !!!!

    I worked it out ........... partly thanks to you mutant fruit !!
    I assumed that since I didn't specify a path for the file I was saving the
    information to, that the file would be created where the exe is,
    but setting the folderbrowserdialog, actually sets the default folder
    to where "pathless" files are saved to the folder selected - if you know what I
    mean.

    So I was saving the path information, into a file which was save in the directory specified by the path information, which wasn't what I expected.

    Anyway - all I need to do is specify a path and it should be fine !!

    Cheers
    Colm


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    If you want to make sure that the files get saved to the same location as your .exe, use "Application.StartupPath + "\\filename.txt"" to retrieve your applications startup path and create a file called "filename.txt" in that directory.


  • Closed Accounts Posts: 85 ✭✭protos


    Cheers. Its working fine now.


Advertisement