Solved

VB.Net Select Specific File

Posted on 2009-07-09
3
267 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 480 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 20 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

695 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