OpenFileDialog FileName cannot be null

brewer

New member
Joined
Apr 23, 2013
Messages
3
Programming Experience
1-3
Trying to read all text files in a directory and then place the contents into a textbox which its property is listed as multiple lines. However, when using this code, there is an error argument thrown 'value cannot be null. Parameter name: FileName'
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mnuFileOpen_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mnuFileOpen.Click, tsOpen.Click[/SIZE]

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] openfiledialog1 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OpenFileDialog[/SIZE]
[SIZE=2]OpenFileDialog1.InitialDirectory = {directory}
[/SIZE][SIZE=2]OpenFileDialog1.Title =[/SIZE] [SIZE=2][COLOR=#a31515]"Open a Text File"[/COLOR][/SIZE]
[SIZE=2]OpenFileDialog1.Filter = [COLOR=#a31515]"Text Files|*.txt|All Files|*.*"[/COLOR]
 OpenFileDialog1.FileName = [COLOR=#a31515]""[/COLOR]
[COLOR=#0000ff]For[/COLOR] i [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Integer[/COLOR][/COLOR] = 0 [COLOR=#0000ff][COLOR=#0000ff]To[/COLOR][/COLOR] OpenFileDialog1.FileNames.Count - 1
[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][SIZE=2] strm [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.IO.Stream[/SIZE]
[/SIZE][SIZE=2] s[/SIZE][SIZE=2]trm = openfiledialog1.OpenFile   [COLOR=#800000] 'exception thrown here[/COLOR]
TextBox1.Text = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Join(Environment.NewLine, OpenFileDialog1.FileNames)[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IF Not (strm is Nothing) then
      'working code here
end if
[/COLOR][/SIZE]end sub
[/COLOR][/SIZE]

Any ideas on how to cure would be great. Thank you.
 
Last edited:
Hi,

You seem to be trying to do two different things in the same routine?

1) Read a selected file through a FileStream
2) Read multiple file names into a TextBox

So which is it?

Either way, you do not show the OpenFileDialog and therefore you do not select any files to be worked with.

If you want your Stream to work then you need to select a file first and then do what you need with the file.

If you want to append a list of selected file names to a TextBox then you need to set the OpenFileDialog MultiSelect property to True to be able to select multiple files and then you could say:-

VB.NET:
TextBox1.Text = String.Join(Environment.NewLine, OpenFileDialog1.FileNames)

Hope that helps.

Cheers,

Ian
 
What I am trying to accomplish is to read all text files in a directory, parse them and add them to a db. Therefore the purpose is to read all files. The textbox was inserted to check to see if the read worked, which it did not since an exception was thrown on the line indicated.

Once past that portion, then the rest can be coded.

took out the portion of textbox and results are the same.
 
If you want to read all text files in a folder then the first thing to do is get rid of that OpenFileDialog and use a FolderBrowserDialog. Then, e.g.:
VB.NET:
For Each textFilePath In IO.Directory.GetFiles(myFolderBrowserDialog.SelectedPath, "*.txt")
    MessageBox.Show(IO.File.ReadAllText(textFilePath))
Next
 
changed the code to:
VB.NET:
[SIZE=2]        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FolderBD [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FolderBrowserDialog[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sFiles [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"C:\Program Files (x86)\Everybody's Phone Company\Ace"[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]FolderBD.SelectedPath = sFiles        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FolderBD.ShowDialog = Windows.Forms.DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]       MessageBox.Show(IO.File.ReadAllText(sFiles)) 'access to path denied'
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE]
permissions to the 'Everybody's Phone Company\ace' directory are 'full'.
How to cure?
Thanks.
 
Hi,

Your variable "sFiles" relates to a Directory name so:-

VB.NET:
IO.File.ReadAllText(sfiles)

is never going to work since this is not a file.

Re-Read post #4 again to see where you went wrong.

Hope that helps.

Cheers,

Ian
 
Back
Top