Results 1 to 21 of 21

Thread: Help needed with a few lines of code (simple code for most of you).

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Question Help needed with a few lines of code (simple code for most of you).

    I am developing a Windows application program using Visual Studio 2017. Hopefully this is the correct forum, if not, I apologize.

    I want a button to execute an .exe program that is either stored in Program Files or Program Files (x86) folder.

    I have no problem to specify one location (e.g. Program Files) but seem to be having problems with searching for the program in the second location (e.g. Program Files (x86), if not found in the first location.

    Your help would be greatly appreciated. My code is as follows (as is obvious, I also have a reminder pop-up and a message pop-up). I've only included the code that succeeded:

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

    Dim Key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\DontAskAgain1")

    If Key Is Nothing Then

    Reminder1.ShowDialog()

    End If

    Try
    System.Diagnostics.Process.Start("C:\Program Files\Malwarebytes Anti-Malware\mbam.exe")

    Catch ex As Exception
    MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the AZFL 'Info' page for program information and download.",
    MsgBoxStyle.Exclamation,
    "AZFL cannot find Malwarebytes' Anti-Malware")

    End Try

    End Sub

  2. #2
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: Help needed with a few lines of code (simple code for most of you).

    Code:
    If My.Computer.FileSystem.FileExists("C:\Program Files\Malwarebytes Anti-Malware\mbam.exe") Then
                System.Diagnostics.Process.Start("C:\Program Files\Malwarebytes Anti-Malware\mbam.exe")
            Else
                MsgBox("File not found.")
                System.Diagnostics.Process.Start("C:\Program Files (x86)\Malwarebytes Anti-Malware\mbam.exe")
            End If
    is this what you want?
    Please Mark your Thread "Resolved", if the problem is solved & Rate those who have helped you

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by AceDuk View Post
    is this what you want?
    Thanks for the help AceDuk. I don't have access to my project right now but I'll try it later this evening. The code you use (i.e. If My.Computer.FileSystem.FileExists) is different than what I'm used to seeing but, it is actually easier for me to understand. Maybe the difference in code is because in 2008 I created my project in Visual Studio 2008 and I am now making changes to that same project using Visual Studio 2017.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by razz3333 View Post
    Maybe the difference in code is because in 2008 I created my project in Visual Studio 2008 and I am now making changes to that same project using Visual Studio 2017.
    No, that's not the difference. The My namespace has been available since VB 2005 and most code written in VB 2008 is almost guaranteed to work as is in VB 2017.

    If you want to do one thing or another then it should be fairly obvious that you need an If...Else statement. That's exactly what they are for. Mind you, you probably ought to use If...ElseIf, in case the file doesn't exist in either location. Personally, I would tend to use IO.File.Exists rather than My.Computer.FileSystem.FileExists but they both do the same thing so it's no big deal. I would just suggest that you be consistent in either always using My or not, rather than mixing and matching. Also, you shouldn't hard-code the paths to the program files folders.
    vb.net Code:
    1. Dim relativePath = "Malwarebytes Anti-Malware\mbam.exe"
    2. Dim x64Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), relativePath)
    3. Dim x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), relativePath)
    4.  
    5. If File.Exists(x64Path) Then
    6.     Process.Start(x64Path)
    7. ElseIf File.Exists(x86Path) Then
    8.     Process.Start(x86Path)
    9. End If

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Thank you jmcilhinney for your help, I really appreciate it. Being a novice to all this programming stuff, I envy all the knowledge you programmer guys have!

    I have not been able to try your code just yet, but will be able to soon. In the meantime I have two questions:

    (1) You said "you shouldn't hard-code the paths to the program files folders", thus will using your code allow my program to find any particular file (in this case "Malwarebytes Anti-Malware\mbam.exe") no matter what folder it's in?

    (2) I've decided to do away with my reminder - i.e. I deleted the following from my previous code:

    Code:
    Dim Key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\DontAskAgain1")
    If Key Is Nothing Then
    Reminder1.ShowDialog()
    End If
    Thus, should my code now be the following?:

    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    
    Dim relativePath = "Malwarebytes Anti-Malware\mbam.exe"
    Dim x64Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), relativePath)
    Dim x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), relativePath)
     
    If File.Exists(x64Path) Then
        Process.Start(x64Path)
    ElseIf File.Exists(x86Path) Then
        Process.Start(x86Path)
    End If
    
    Catch ex As Exception
    MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the AZFL 'Info' page for program information and download.",
    MsgBoxStyle.Exclamation,
    "AZFL cannot find Malwarebytes' Anti-Malware")
    
    End Sub
    Last edited by razz3333; Apr 23rd, 2017 at 08:26 AM.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by razz3333 View Post
    (1) You said "you shouldn't hard-code the paths to the program files folders", thus will using your code allow my program to find any particular file (in this case "Malwarebytes Anti-Malware\mbam.exe") no matter what folder it's in?
    No, it will make sure that you are looking in the actual program files folders.

    If somebody speaks a different language, their program files folders may have different names (basically "program files" in their language).

    Different versions of Windows can use different names and/or locations for folders.

    It is possible to move folders like program files, for example I have in the past had it on the D drive.


    If you use jmcilhinney's suggestion, you don't need to worry about any of those issues.

    (2) ....
    Thus, should my code now be the following?:
    Almost, you seem to have lost a few lines (such as: Try).

    It would also be a good idea to add an Else clause, eg:
    Code:
    ElseIf File.Exists(x86Path) Then
        Process.Start(x86Path)
    Else
        Msgbox "Not found"
    End If

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Thank you for your help si_the_geek. Should the "Try" and "End Try" be located as follows?:

    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Try
    Dim relativePath = "Malwarebytes Anti-Malware\mbam.exe"
    Code:
    ElseIf File.Exists(x86Path) Then
        Process.Start(x86Path)
    Else
        Msgbox "Not found"
    End If
    End Try
    Also: do I delete: "Catch ex As Exception" just before Msgbox?

    One more question: What is the exact name for this code? - in case I want to try and find a course in it.
    Last edited by razz3333; Apr 23rd, 2017 at 09:22 AM.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Help needed with a few lines of code (simple code for most of you).

    That is a good position for the Try, but the End Try needs to be after the entire "Catch" section (so after the MsgBox).

    the "Try, Catch" is known as Exception Handling, you can see the help for it here:
    https://msdn.microsoft.com/en-us/library/fk6t46tz.aspx

    In simple terms, you should have something like this:
    Code:
    Try  
        'code here that might have an unexpected error (aka exception)
    
    Catch ex As Exception
        'code here that runs only if there is an error/exception
        '(preferably using ex.Message, so that you know what the actual problem was)
    End Try

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Thank you again, si_the_geek. I'll be trying this code within my project in a few hours from now. I'll post again in this tread when I see it's working, or I guess also if I'm having a problem still

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    OK, still having a problem :-( - what follows is my current code with what I had to change (as per fix suggestions from Visual Studio 2017) marked in Red:
    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    
            Try
    
                Dim relativePath = "Malwarebytes Anti-Malware\mbam.exe"
                Dim x64Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), relativePath)
                Dim x86Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles(X86)), relativePath)
    
                If IO.File.Exists(x64Path) Then
                    Process.Start(x64Path)
                ElseIf IO.File.Exists(x86Path) Then
                    Process.Start(x86Path)
                Else
                    MsgBox("Not found")
                End If
    
            Catch ex As Exception
                MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the AZFL 'Info' page for program information and download.",
             MsgBoxStyle.Exclamation,
             "AZFL cannot find Malwarebytes' Anti-Malware")
    
            End Try
    
        End Sub

    According to the red underline comment in Visual Studio, "Environment.SpecialFolder.ProgramFiles(x86)" cannot be indexed because it has no default value.

    What do I need to do to rectify this?

    Thanks again for your time and effort.
    Last edited by razz3333; Apr 23rd, 2017 at 04:21 PM.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Help needed with a few lines of code (simple code for most of you).

    Remove the brackets from ProgramFiles(x86) , it should be ProgramFilesX86

    (if that doesn't work for you, remove the dot before it, then type the dot again and see what options you get)

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Code:
     Dim x86Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesx86), relativePath)
    Now it says: 'ProgramFilesx86' is not a member of 'Environment.SpecialFolder'.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    There was no need to use 'IO' over and over. It's not wrong but, when using types from the same namespace multiple times, it is generally considered good practice to import that namespace. That's what I did when writing my example and that's why I didn't have to qualify File or Path.

    As for the Environment.SpecialFolder.ProgramFilesX86 field, it was added in .NET 4.0, according to the documentation. That would suggest that you're using a .NET version earlier than that. It's always a good idea to specify that you're using an older version because people will generally assume the latest otherwise. In that case, it should generally be safe to do this:
    vb.net Code:
    1. Dim programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & " (x86)"

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by jmcilhinney View Post
    As for the Environment.SpecialFolder.ProgramFilesX86 field, it was added in .NET 4.0, according to the documentation. That would suggest that you're using a .NET version earlier than that.
    I inserted the code you gave and it showed errors. I've attached an image below to best show all.

    Name:  for Forum.jpg
Views: 161
Size:  35.6 KB
    Last edited by razz3333; Apr 23rd, 2017 at 08:30 PM. Reason: deleted a sentence due to misunderstanding.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by razz3333 View Post
    How is the best way to check what .NET version I have and is it easy to update to at least .NET 4.0?
    Why do you have to check? You were prompted for a version when you created your project so you should already know. As with anything else to do with the properties of your project, you can view and edit the .NET version being targeted in the project properties.
    Quote Originally Posted by razz3333 View Post
    I inserted the code you gave and it showed errors.
    That's because you didn't think about what you were doing. It's telling you that 'x86Path' isn't declared because you removed the code that was declaring it. This is that code:
    Code:
    Dim x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), relativePath)
    The highlighted part is what was supposed to provide path of the 'Program Files (x86)' folder but that doesn't work in your case. I gave you this code to get that path:
    Code:
    Dim programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & " (x86)"
    If that 'programFilesX86' now contains the path of the 'Program Files (x86)' folder, how should you change the first code snippet?

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    My current code is the following, please show me where I am supposed to declare the x86 path.
    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    
            Try
    
                Dim relativePath = "Malwarebytes Anti-Malware\mbam.exe"
                Dim x64Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), relativePath)
                Dim programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & " (x86)"
    
                If IO.File.Exists(x64Path) Then
                    Process.Start(x64Path)
                ElseIf IO.File.Exists(x86Path) Then
                    Process.Start(x86Path)
                Else
                    MsgBox("Not found")
                End If
    
            Catch ex As Exception
                MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the AZFL 'Info' page for program information and download.",
             MsgBoxStyle.Exclamation,
             "AZFL cannot find Malwarebytes' Anti-Malware")
    
            End Try
    
        End Sub
    Thanks again for helping. Sorry if I am having some trouble understanding.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    I gave you this code:
    Code:
    Dim x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), relativePath)
    The highlighted part represents the path of a folder but it doesn;t work for you, so you need something else to represent that path. I gave you this code:
    Code:
    Dim programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & " (x86)"
    The highlighted part now represents the path to that same folder. You need to use the second code snippet to change the first to work for you. I'm not going to do that for you because you should be able to do that for yourself. You may be new to programming but you're not new to logic. If you can't apply logic then you've got bigger problems than this bit of code.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Thanks for your help jmcilhinney. In regards to your last sentence, there was no need for you to be rude.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by razz3333 View Post
    In regards to your last sentence, there was no need for you to be rude.
    It was a statement of fact. You're not going to make a software developer of any kind if you can't follow a simple train of logic like that. It shouldn't have been necessary for me to go into so much detail for something that simple. If you don't exercise your mind to make such logical steps then you're never going to be able to make a proper fist of writing code. Not everyone has what it takes to write software and there's no shame in being one of the ones who doesn't. There are plenty of things that I don't have what it takes to do well. If you think you have what it takes to write software then don't sit around waiting for people to explain simple things like this because it YOU, not me, who will be the one to suffer as a result.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by jmcilhinney View Post
    You're not going to make a software developer of any kind
    You have forgotten one VERY important point. Not every forum member is here for the purpose of being taught coding. Some, with the help of sophisticated software, are just trying to create one simple program for their own personal use. They will never again be coding anything. Even with using coding software, perhaps they need help with just one or two lines of code. They join this forum expecting to get a little help without being insulted.

    Just so you know, your teaching methods leave a lot to be desired. I could write a full page detailing why this is so (in my line of work, I know 'good' teaching from 'bad' teaching) but it would not be logical for me to waste my time doing that. Instead, after my next sentence, I will be very logical and simply end my part in this thread.

    One last thing, AceDuk and si_the_geek, I want to sincerely thank you both for your help. I really appreciated it.

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help needed with a few lines of code (simple code for most of you).

    Quote Originally Posted by razz3333 View Post
    You have forgotten one VERY important point. Not every forum member is here for the purpose of being taught coding. Some, with the help of sophisticated software, are just trying to create one simple program for their own personal use. They will never again be coding anything. Even with using coding software, perhaps they need help with just one or two lines of code. They join this forum expecting to get a little help without being insulted.
    I haven't forgotten that at all. I'm quite cognisant of that fact. What you probably haven't forgotten because you probably never considered it is that not everyone is interested in doing the work of people like that for them. If you're too lazy to think through a simple problem because you don't want to be a programmer then why should others do your thinking for you? Some will and that's their prerogative but others will not. You can come here with whatever expectation you like but that doesn't mean that it will, or should, be met. I come here with the expectation that people will do what they can for themselves before asking complete strangers to do something for them but I'm regularly disappointed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width