?
Solved

VB.Net Select Specific File

Posted on 2009-07-09
3
Medium Priority
?
268 Views
Last Modified: 2012-08-14
Hey everyone,

I want to prompt the user to select a file, an exact file, for example "MyFile.ext" - from a folder - I know it can be done, I just don't see how quite yet.

The user should NOT be able to change the file name (MyFile to OtherFile, WrongFile, etc.)
The user should NOT be able to change the file filter (changing *.ext to *.txt, *.doc, etc.)

I want it to only accept if the correct file is chosen.

I will be in to look at answers in the morning ~ 12 hours or so.

Thanks!
0
Comment
Question by:Crazy_Penguins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 1920 total points
ID: 24820411
Use the normal file selection dialog boxes.

You can set the box to show only certain filenames or file filters, however you cannot FORCE the use to use it. They can always type "*" into the window and see all files, and select one of them. Also, you can't force the user to pick from a specific directory. They can always type ".." or a full path into the box and navigate to another folder.

However, once the window closes, you can check what the returned file was and if it wasn't right, pop up an error and make them try again. Use the Path.xxxx functions in the System.IO.Path class to check that the filename and path was what you expected, and use the File.xxxxx functions in the System.IO.File class to check that the file really exists, and to eventually open it.

http://www.startvbdotnet.com/controls/dialog.aspx

Use the System.Windows.Forms.OpenFileDialog class to show the generic explorer-style open dialog box to the user:

- Initialize a new instance of the class
- Set the filter property to "myfile.ext|myfile.ext"
- Set the initialdirectory property to whatever folder you want the dialog box to start in
- Show the dialog box using the ShowDialog() function
- When the box returns, check the filename property and pop up a messagebox if it was wrong.

      If File.Exists(thefile) And Path.GetFileName(thefile).ToLower() = "myfile.ext" Then
         .... worked ...
      Else
        .... fail ...
      End If
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 80 total points
ID: 24822569
You can use the FolderSelection dialog to allow the user select the folder only. Otherwise you could inherit from the openfiledialog and add the desired functionality yourself.
0
 

Author Comment

by:Crazy_Penguins
ID: 24825102
Frosty555: Thanks for your input - sounds like what I need.

Full story is I am doing some scripting with GHOST32.exe (Symantec Ghost) and need to know where the file is, before I try to use it.

I will use what you said, then after I know the file is there, use the file len function to check size of file (to a range that's close to what I am expecting)

CodeCruiser: Thanks for your input - true once could inherit - but I am choosing Frosty555's method.

Thanks both!

Andrew
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question