ASP.NET VB.NET to have DirectoryInfo and retrieve files with spaces.

I have a ASP.NET website that we use for our client portal.  It is written in VB.  I created a DirectoryInfo link that works great and gives the users the ability to see a directory listing and download the file.  I have to make sure that the files in the directory do not have spaces or the link gives an error.  Is there any way to change the code to retrieve the file that has a space in it  Below is an example of my code.  Any help would be appreciated.

  Protected Sub Label11_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label9.Load

        Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("e:\printed statements\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\")
        Dim aryFi As IO.FileInfo() = di.GetFiles
        Dim fi As IO.FileInfo

        For Each fi In aryFi
            strFileSize = (Math.Round(fi.Length / 1024)).ToString()
            Label11.Text += "<a href=https://www.brandytrust.net\Clientfiles\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\" + fi.Name + ">" + fi.Name + "</a><br /> "

        Next

Open in new window

bidgadgetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul MacDonaldDirector, Information SystemsCommented:
Replace the spaces in the file name with %20.

So if the file name is "My Text File.txt", instead of

"www.mysite.tld/My Text File.txt"

have it read

"www.mysite.tld/My%20Text%20File.txt"

ASP.Net as the URLEncode method that will do this for you, but a simple string substitution will work for you just as well.

and you should be okay.
bidgadgetAuthor Commented:
there are hundreds of files in hundreds of directories.  Is there any way to retrieve without having to change all the actual file names manually.
bidgadgetAuthor Commented:
sorry I am confused.   Are you saying to manually change each file and take out the spaces or do I need to but %20 in my line of code?  Thank you
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Paul MacDonaldDirector, Information SystemsCommented:
I'm not suggesting you change file names.

You asked how to filter out file names with spaces, because when you send people a link to a file  name with spaces, the link doesn't work.  My response was to show you how to make those links work.

The file names can stay the same.  You can show the user the file names as they actually appear.  When the user wants to download a file with a space in the file name, change the URL so any spaces are replaced with "%20", and the link should work just fine.
bidgadgetAuthor Commented:
OK, thank you.  I appreciate your suggestion.  Unfortunately we cannot ask all of our clients to change url links.  I was hoping there was a way to have the link add in the %20 automatically if it sees a space in the file name.  I guess there is not way to do that?
Paul MacDonaldDirector, Information SystemsCommented:
I don't know what you mean by "automatically".  I'm suggesting a small change to the code you're using, which is "automatic" from a human perspective.  No intervention would be required on the file.  You'd simply be fixing the link to the file so it would work.
bidgadgetAuthor Commented:
ok, thank you.  I guess I don't know where in my code I would replace the code
+ fi.Name + ">" + fi.Name +

So if the above is the part in my code that retrieves the file name, where would I change it?  Thank you and so sorry for the confusion.
Paul MacDonaldDirector, Information SystemsCommented:
I believe this should work:


    Protected Sub Label11_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label9.Load

        Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("e:\printed statements\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\")
        Dim aryFi As IO.FileInfo() = di.GetFiles
        Dim fi As IO.FileInfo

        For Each fi In aryFi
            strFileSize = (Math.Round(fi.Length / 1024)).ToString()
            Label11.Text += "<a href=https://www.brandytrust.net\Clientfiles\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\" + Replace(fi.Name, " ", "%20") + ">" + fi.Name + "</a><br /> "

        Next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bidgadgetAuthor Commented:
Thank you so much!
Paul MacDonaldDirector, Information SystemsCommented:
Very happy to help.
bidgadgetAuthor Commented:
And it worked perfectly. Cheers!
Paul MacDonaldDirector, Information SystemsCommented:
Ha ha ha!  I took that for granted as you'd closed the question.  From now on, make sure you have a working solution before you award points!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.