4 Replies - 2374 Views - Last Post: 20 June 2010 - 05:33 PM Rate Topic: -----

#1 ShoSho_1489   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-November 09

Selecting Multiple files

Posted 20 June 2010 - 12:59 AM

I'm trying to select multiple files, then display those files in a Rich text box. Whenever I append the name to the rich text box, it only prints "System.String[]" The number of files is correct, but it's not putting the name. As I understand, when using the FileNames option, it puts the different file names into a string array so I attempted to use a foreach loop without much success. The output i'm looking for is:

Filename1
Filename2
Filename3
..
etc.

Any help is appreciated. :)
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog browse = new OpenFileDialog();
            browse.Title = "Select File(s)";
            browse.Multiselect = true;
            browse.InitialDirectory = "F:\\";

            if (browse.ShowDialog() == DialogResult.OK)
            {
                //txtFileName.Text = browse.FileName;

                foreach (String file in browse.FileNames)
                {
                    txtbxFileContent.Text += "\n" + browse.FileNames;
                }
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Selecting Multiple files

#2 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: Selecting Multiple files

Posted 20 June 2010 - 01:10 AM

You should changed your line to:
textbxFileContent.Text += "\n" + file;



With your current code, you're printing out the array of files rather then the actual values of each item in the array.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Selecting Multiple files

Posted 20 June 2010 - 08:26 AM

You're also making life harder than it needs to be. If you have the choice, don't use a richtextbox. You have a list of files, use a listbox. Then you just add an item. That way you can also select one or more items, delete them, resort em etc.
Was This Post Helpful? 1
  • +
  • -

#4 ShoSho_1489   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-November 09

Re: Selecting Multiple files

Posted 20 June 2010 - 03:04 PM

View PosttlhIn, on 20 June 2010 - 07:26 AM, said:

You're also making life harder than it needs to be. If you have the choice, don't use a richtextbox. You have a list of files, use a listbox. Then you just add an item. That way you can also select one or more items, delete them, resort em etc.

I took your advice and redid it all with a listbox, however when I go to list the contents of subdirectories it tells me I don't have access to 3 subdirectories that aren't there, at least, I don't see them and they aren't hidden. So I searched the msdn library for some method that determined if a specified directory could be accessed and didn't have much luck. I would like to have my code access and list the files in a subdirectory only if it has access to it, would it be possible? Right now I just have a try catch to pick up the exception and display a message box.

        private void btnManBackup_Click(object sender, EventArgs e)
        {
            string folderPath = "C:\\Users\\Owner\\Documents";

            ProcessDirectory(folderPath,lstFiles);
        }
        public static void ProcessDirectory(string targetDirectory, ListBox lstFiles)
        {
            // Process the list of files found in the directory.
            try
            {
              //If permission to access directory == true   
              //{
                    string[] fileEntries = Directory.GetFiles(targetDirectory);
                    foreach (string fileName in fileEntries)
                    {
                        lstFiles.Items.Add(fileName);
                    }
                    // Recurse into subdirectories of this directory.
                    string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
                    foreach (string subdirectory in subdirectoryEntries)
                        ProcessDirectory(subdirectory, lstFiles);
              //}
            }
            catch (Exception e)
            {
                MessageBox.Show("Authority to the directory" + targetDirectory + " is                   denied.","Error",MessageBoxButtons.OK);
            }
            finally { }

        }


Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Selecting Multiple files

Posted 20 June 2010 - 05:33 PM

I'm sure someone will pop up with a way of using Active directory or some other service yo check if you have permission, but personally I would do what you have done, minus e message box: make an effort to read from the directory and if it fails en you don't have permission. It certainly does the job regardless of a network or local volume, Window, Unix or Macintosh share, VMware virtualized PC, RAM drive, or some device not yet invented that comes out in 3 years - whether or not any of those possibilities support a .NET means of checking. Often times the simplest solution is the most robust.

You might consider returning a value from your method instead of a void. True if success, false if exception. Or return a string: "ok" if works or the .message property of the exception. That way your calling method can react to a problem, or ignore it if there is no need to. It costs you nothing to know if a method fails and you can always ignore it, but 6 months from now you may need to know. Wouldn't it be nice if your code already had the underlaying infrastructure in place?

I think of error returns like guns and condoms: Better to have one and not it, than need it and not have it.

This post has been edited by tlhIn'toq: 20 June 2010 - 05:38 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1