1.

Solve : VB.Net - How to GET file name without opening it using OpenFileDialog??

Answer»

I try to get a file name using OpenFileDialog

The PROBLEM is : I cannot get the file name i want because the file that I want to get its name have no permission to open/read.
(I can get the name of the other file that have read/open permission)

So, in order to get the file name that have no open/read permission, Is there a way to get the name without opening the file.
OR
Is there any other METHOD?

I will be very glad if you can help me..Does something like this not work:

Code: [Select]Dim ofd As New OpenFileDialog()
Dim AcquiredFile As String
ofd.Filter = "Text FILES (*.txt)|*.txt|All Files (*.*)|*.*"
If ofd.ShowDialog(Me) = DialogResult.OK Then
AcquiredFile = ofd.FileName
End If

Also what happens when you select a file that you have no permission for in the Dialog?Here is my problem:
when i try to open the file that i don't have a permission to open/read it, it gives an error saying that i don't have a permission to open the file. If i can get that file name it will be enough...



I attach the screenshot of my error message

[recovering disk space, attachment deleted by admin]hmm, interesting. I wonder if maybe you could use a Save File Dialog instead?

Otherwise the only thing I can think of might require you to actually use the low-level GetOpenFileName() function.Not easy at all....

[recovering disk space, attachment deleted by admin]I could not do it using save file dialog, If there is GetNameDialog it would be very easy

I try many times, different ways but still.... I can't do it..

I also try the following code:

Dim ff As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String

ff.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"

If ff.ShowDialog() = DialogResult.Cancel Then
strFileName = ff.FileName
MsgBox(strFileName) ' Here it does not return anything if i USED DialogResult.Cancel
End If



I give-up on this one


Why is not possible to get the filename without opening the file ?
"That's the Question" If you are targeting Vista or Later, you can use the Windows API Code pack. The "CommonFileDialog" seems to work:

Code: [Select]Dim cofd As New CommonOpenFileDialog
Dim AcquiredFileName As String
If cofd.ShowDialog = CommonFileDialogResult.Ok Then
AcquiredFileName = cofd.FileName
End If

I was able to reproduce the original issue by changing some perms on a file. I have set global deny permissions on the file but this now works (with the API code pack) to get the filename.

It's worth noting that the the OpenFileDialog a SaveFileDialog do not open or save EITHER. They just don't allow the selection of items for which the user has no permissions to perform any action on, because the assumption is that the application is going to do something with the file. how do i make use of the run command



Discussion

No Comment Found