Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My Program When I click on the Listview Item it freezes and gives the error O The system cannot find the file specified

VB.net

What I have tried:

I am Developing an Operating System Simulator and the Application Window When I Click on an App It gives an Error that did not find the App File Please Help Me

[EDIT]below piece of code has been added from comment[/EDIT]

Here is the part of the code where the error is
VB
Private Sub Apps_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each files As String In Directory.GetFiles(".\apps\")
ListView1.Items.Add(Path.GetFileName(files))
Next

End Sub

Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
For Each file As ListViewItem In ListView1.Items
Dim filePath As String = file.SubItems(0).Text + "\" + file.Text
If file.Selected = True Then
Process.Start(filePath)
End If
Next
End Sub
Posted
Updated 30-Jan-20 22:20pm
v2
Comments
Dave Kreskowiak 30-Jan-20 22:43pm    
It's not possible to answer this at all. We have no idea what the code is doing at the time you click on the ListView. The ListView itself will not throw that error so it has to be something in your super-secret code that you didn't share with us.
Hawk2811 30-Jan-20 23:16pm    
Here is the part of the code where the error is

Private Sub Apps_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each files As String In Directory.GetFiles(".\apps\")
ListView1.Items.Add(Path.GetFileName(files))
Next

End Sub

Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
For Each file As ListViewItem In ListView1.Items
Dim filePath As String = file.SubItems(0).Text + "\" + file.Text
If file.Selected = True Then
Process.Start(filePath)
End If
Next
End Sub
Dave Kreskowiak 31-Jan-20 0:34am    
First, in Apps_Load, your code is making assumptions about what the "current directory" is. ALWAYS build fully qualified paths from a well-known folder, Environment.GetFolderPath Method (System) | Microsoft Docs[^], or from the folder your app is running from, How To Get Application Startup Path From The Console Application Using C#[^].

This is also true for ListView1_DoubleClick. Again, your code is assuming what the "current directory" is. Never do that. The current directory can be modified without you knowing it, like if you show an OpenFileDialog and the user starts going through folders looking for a file.
Hawk2811 31-Jan-20 1:55am    
this and in C # or vb
Dave Kreskowiak 31-Jan-20 7:21am    
I have no idea what you just said.

If you're asking me to write your code for you, that's not going to happen. Nobody writes my code for me, and I have a lot of work to do, while I'm writing yours.

VB
For Each file As ListViewItem In ListView1.Items
Dim filePath As String = file.SubItems(0).Text + "\" + file.Text
You take each filename and append it a path separator plus the filename again. This leads to an inexistent file.
Put a breakpoint on the For Each line, press F5, and start a debug session. Watch carefully for the value of the filePath variable and you will understand the issue.
 
Share this answer
 
We can't tell you "do this and it'll work" - we have no idea what is in your Listview, and no access to your file system! And you need both in order to work out what is going on.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Pay close attention to teh Listview content and to the path you generate. Then compare that exact path against your real file system, and see where your file is.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900