Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.Net Select Specific File

Posted on 2009-07-09
3
Medium Priority
?
269 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

604 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