C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2009, 07:40 AM
lambono lambono is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 8 lambono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 19 sec
Reputation Power: 0
Multi File Dialogue

Hi,
How do you retrieve the different properties of multiple files using OpenFileDialog?

E.G the following code gets the FileName property for each selected file

Code:
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;

   if (openFileDialog1.ShowDialog() == DialogResult.OK)
   {
       foreach (String fileName in openFileDialog1.FileNames)
          {
              // process fileName
          }
   }



But how would I get the FileName and SafeFileName for each selected file?

Thanks for your help!

Reply With Quote
  #2  
Old July 2nd, 2009, 02:50 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,382 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 17 m 29 sec
Reputation Power: 563
If by safe you mean the short FAT file system compatible version; there is a Win32 API that will return the short path/file when given a full NTFS path/file. I don't recall what it is off of the top of my head and I am sure there's a .NET equivalent for it. Does your File Dialog even show the short file names? I kind of doubt that it would. There's no reason for it to be an attribute of the file. Pretty sure there's just a simple algorithm they apply to a real path/file name to come up with the short one.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #3  
Old July 2nd, 2009, 06:55 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,382 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 17 m 29 sec
Reputation Power: 563
You should find some answers here:

http://msdn.microsoft.com/en-us/lib...247(VS.85).aspx

http://msdn.microsoft.com/en-us/lib...989(VS.85).aspx


I am still investigating whether there is a pure .NET solution for you.

Reply With Quote
  #4  
Old July 3rd, 2009, 05:59 AM
lambono lambono is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 8 lambono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 19 sec
Reputation Power: 0
Hi jwdonahue,
Thanks for your help!

In this case I am looking for the File Path and File Name.

These can be retrieved from the openFileDialog object properties
ie.
String fileName = openFileDialog1.SafeFileName
String fileNameWithPath = openFileDialog1.FileName

This works when one file is selected.

When 2 or more files are selected you use openFileDialog1.SafeFileNames and openFileDialog1.FileNames which return string arrays.

The following code works to process both string arrays. However I am sure there must be a much more elegant way to get the same result and avoid the 3 different loops?? Please let me know if you have any thoughts.

Code:
        private void btnAddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
            OpenFileDialog1.Multiselect = true;
    
            // Add Selected Files from the OpenFileDialog
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int loopCounter = 0;
                int intNumberFilesSelected = openFileDialog1.FileNames.GetLength(0);

                String[] fileNames = new String[intNumberFilesSelected];
                String[] safeFileNames = new String[intNumberFilesSelected];

              
                // Retrieve File Name Including File Path
                foreach (String fileName in openFileDialog1.FileNames)
                {
                    fileNames[loopCounter] = fileName;
                    loopCounter++;
                }
                loopCounter = 0;

                // Retrieve File Name
                foreach (String safeFileName in openFileDialog1.SafeFileNames)
                {
                    safeFileNames[loopCounter] = safeFileName;
                    loopCounter++;
                }

                // Add Items to ListView
                for (int i = 0; i < intNumberFilesSelected; i++)
                {
                    ListViewItem lviFile = lvwFiles.Items.Add(safeFileNames[i]);
                    lviFile.SubItems.Add(fileNames[i]);
                }
            }

        }


Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Multi File Dialogue


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT