简体   繁体   中英

vb.net wildcard file search exception

im totally new to vb.net

I figured how to find files using wildcard and it works fine but i need error meassage if file not found. here's my code. any help highly appreciated !

    For Each hist In Directory.GetFiles("C:\temp", "*.*", SearchOption.TopDirectoryOnly)
        If File.Exists(hist) Then
            File.Copy(hist, Path.Combine("C:\temp\1", Path.GetFileName(hist)), True)
            MessageBox.Show("file exist and copied") <-- this message shows up and files are copied

        Else

            MessageBox.Show("No files. Folder is empty !") <--this message never shows up when folder is empty . no files at all


        End If

    Next

It is unlikely you would see that second message. You are grabbing the list of files from the OS directly - so, at least at the time that you retrieve the file name that file exists. If you wanted to simulate a case where the file doesn't exist, place a break point on the IF File.Exists(hist) Then line. While the program is paused there, go find and delete whatever the current file is. Then continue the program.

i got it working. here's what i added before foreach :

Dim myDir As DirectoryInfo = New DirectoryInfo("c:\temp\")
        If (myDir.EnumerateFiles().Any()) Then


 foreach ....


else

MessageBox.Show("no files in directory ")   <-- my message

thanks for advice!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM