Solved

VB.Net Select Specific File

Posted on 2009-07-09
3
262 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now