Solved

VB.Net Select Specific File

Posted on 2009-07-09
3
266 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

738 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