Results 1 to 23 of 23

Thread: How do you code the execution of a file that goes by two names?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Question How do you code the execution of a file that goes by two names?

    I'm developing a windows application using .NET Framework 4.0. Being a novice to all this development stuff, I'm not sure of the terminology in regards to "using", in reference to the first sentence. Please correct the terminology so I will know in the future.

    Is there an easy way to code the execution of a file that goes by two names when installed by users? In this particular case, the program "Speccy". To clarify my question, see image.

    Name:  Speccy.PNG
Views: 326
Size:  4.4 KB

    I currently coded to search for "Speccy64" in both Program Files and Program Files (x86) - i.e. not coded to search for "Speccy"

    Code:
    Try
                Dim relativePath = "Speccy\Speccy64.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("Speccy not found")
                End If
    
            Catch ex As Exception
                MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the 'Program List' page for program information and download.",
             MsgBoxStyle.Exclamation,
             "AZFL cannot find Speccy")
    
            End Try

  2. #2
    Lively Member
    Join Date
    Jun 2017
    Posts
    77

    Re: How do you code the execution of a file that goes by two names?

    I think a better solution would be to check if Windows is 32 or 64 bit.

    If it's 32bit version, then open Speccy.exe. If it's 64bit, then open Speccy64.exe.

    You can probably make use of Environment.Is64BitOperatingSystem to check the version.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by BlackRiver1987 View Post
    You can probably make use of Environment.Is64BitOperatingSystem to check the version.
    Thank you for your help BlackRiver1987. I have what is probably a silly question (shows my extreme novice programming mind):
    Can you use If and Else in this case? - i.e.:
    Code:
    If Environment.Is64BitOperatingSystem then
    Process.Start(x64Path) else
    Process.Start(x86Path)

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: How do you code the execution of a file that goes by two names?

    The file doesn't go by two different names, there are two different files.

    but your trying to use the same program for 32 and 64 bit systems,
    Code:
    Dim relativePath = "Speccy\Speccy64.exe"
                Dim x64Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), relativePath)
                Dim x86Path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & " (x86)", relativePath)
    Code:
    If Environment.Is64BitOperatingSystem then
           'use the 64 bit version Speccy64.exe
    Else
          'use the 32 bit version Speccy.exe
    End If

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    I feel I should better explain my issue. Part of the function of my program that I'm developing is to launch various freeware applications. One of these applications being Speccy. Now since my Windows is 64-bit, when I installed Speccy on my laptop, Speccy by default installed to Program Files. Most likely if a user of my program has a 32-bit OS, then when they install Speccy, it would install to Program Files (x86). Due to this, in order for all users to be able to launch Speccy, my program needs to search for Speccy in both Program Files and Program Files (x86). To further complicate things, when a user installs Speccy, two executable files are installed (Speccy.exe and Speccy64.exe) - therefore, my program needs to point to the correct version. This is what I'm trying to accomplish - search both folders and for the corresponding .exe file (most programs only installs the needed .exe file for the specified bit version, but this is not the case with Speccy).

    So at the moment I'm lost as to how to correctly code for both searches. Clicking the "Speccy" button in my program needs to locate where Speccy is installed and then needs to launch the correct version of Speccy.

    As always, your help will be GREATLY appreciated.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: How do you code the execution of a file that goes by two names?

    Try,
    Code:
    Dim myPath as String
    If Environment.Is64BitOperatingSystem then
           myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy64.exe")
    Else
         myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Speccy\Speccy.exe")
    
    End If
    
    MessageBox.Show(myPath)
    Process.Start(myPath)
    I put the MessageBox in there just to verify the path, you can remove it when it's working.

    One strange thing that I did come across is if your project Target CPU is set to x86, this
    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) will return the Program Files (x86) path.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Hi wes4dbt, thanks so much for your time and effort in helping me. Sorry for the delayed response.

    Your code works flawlessly for me - in my case, as mentioned earlier, Speccy is in the Program Files folder and since I have 64-bit, Speccy64.exe is launched.

    Unfortunately I do not have access to a 32-bit system so I can't check if your code works if Speccy is installed in Program Files (x86) and thus needs to launch Speccy.exe. As a test, I tried to simply move the Speccy folder to Program Files (x86), but this does not work and an error message appeared because the file could not be found - I then figured out that would be because the code would only search the Program Files folder since it can see that my system is 64-bit. Please correct me if I'm wrong.

    The message was the same regardless of where I placed the Speccy folder.

    Will this code work if Speccy is in Program Files (x86) and therefore needs to launch Speccy.exe (i.e. not launch Speccy64.exe) ?

    Name:  4forum.PNG
Views: 196
Size:  3.0 KB

    The code I inserted is shown below:

    Code:
     Private Sub Speccy_Click(sender As Object, e As EventArgs) Handles Speccy.Click
    
            Dim myPath As String
            If Environment.Is64BitOperatingSystem Then
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy64.exe")
            Else
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Speccy\Speccy.exe")
    
            End If
    
            MessageBox.Show(myPath)
            Process.Start(myPath)
    
        End Sub
    Last edited by razz3333; Jun 25th, 2017 at 12:33 PM.

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by razz3333 View Post
    Now since my Windows is 64-bit, when I installed Speccy on my laptop, Speccy by default installed to Program Files. Most likely if a user of my program has a 32-bit OS, then when they install Speccy, it would install to Program Files (x86).
    The Program Files (x86) folder only exists on 64bit systems, on a 32 bit system it is just Program Files. The (x86) version is there so on a 64bit system you have separate folders for 64bit software and 32bit software.

    If they always install the 64bit version on 64bit windows then you can always use the Program Files folder, you only need to worry bout the (x86) folder if they have installed the 32bit version on a 64bit OS.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    I now understand, thank you PlausiblyDamp for explaining it to me. I guess some users will install 32bit software on their 64bit OS, either by mistake or because, in the case of some software (most likely older software), the only available version is 32bit. So if this is the case, coding in my program needs to be able to handle this.

    I'm hoping the code listed above will indeed be able to launch Speccy no matter what bit version a user has. Like I mentioned, I have no way to test if the code works on 32bit Windows. I guess I'm just asking for verification from wes4dbt or anyone else that it will indeed work.

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: How do you code the execution of a file that goes by two names?

    PD, is correct

    There is four possibilities

    x64 version installed on a x64 OS
    x86 version installed on a x64 OS
    x86 version installed on a x86 OS
    It's not installed

    I don't have a 32bit OS to test on but this seems correct
    Code:
            Dim myPath As String, isFound As Boolean = False
            If Environment.Is64BitOperatingSystem Then
                'Check if x64 version is installed
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy64.exe")
                If IO.File.Exists(myPath) Then
                    isFound = True
                Else
                    'Check if x86 version installed on a x64 OS
                    myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Speccy\Speccy.exe")
                    If IO.File.Exists(myPath) Then
                        isFound = True
                    End If
                End If
            Else
                'This is a 32 bit OS
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy.exe")
                If IO.File.Exists(myPath) Then
                    isFound = True
                End If
            End If
    
            MessageBox.Show(isFound.ToString)
            If isFound Then
                Process.Start(myPath)
            End If

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    wes4dbt, thanks a million for your code. It works great! I have one last request: I am trying to display a certain message if the program cannot be found (i.e. if it's not installed or installed to some weird folder other than the default install folder). I've been trying to insert it but I just cannot get it to work correctly.

    What I wish to display is:

    Code:
    MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the 'Program List' page for program information and download.",
                MsgBoxStyle.Exclamation,
                "AZFL cannot find Speccy")
    I really do appreciate your time and effort.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Silly me! I figured out how (marked in red), now it seems really obvious!

    Code:
     Dim myPath As String, isFound As Boolean = False
            If Environment.Is64BitOperatingSystem Then
                'Check if x64 version is installed
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy64.exe")
                If IO.File.Exists(myPath) Then
                    isFound = True
                Else
                    'Check if x86 version installed on a x64 OS
                    myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Speccy\Speccy.exe")
                    If IO.File.Exists(myPath) Then
                        isFound = True
                    End If
                End If
            Else
                'This is a 32 bit OS
                myPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Speccy\Speccy.exe")
                If IO.File.Exists(myPath) Then
                    isFound = True
                End If
            End If
    
            If isFound Then
                Process.Start(myPath)
            Else
    
                MsgBox("Program is either not installed or is installed in the wrong location." & vbNewLine & "View the 'Program List' page for program information and download.",
                MsgBoxStyle.Exclamation,
                "AZFL cannot find Speccy")
    
            End If

  13. #13
    Lively Member
    Join Date
    Jun 2017
    Posts
    77

    Re: How do you code the execution of a file that goes by two names?

    Just a thought...What happens when someone installs Speccy to a different location?

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How do you code the execution of a file that goes by two names?

    That may be a problem that isn't worth solving. If somebody goes and puts the application in some random location, then it really could be almost anywhere, including on a network drive. Searching the entire system looking for the file on the offhand chance that it exists is beyond reason. However, what you could do, rather than just give the messagebox saying the program isn't there, is give them the option of searching for the location. If they accept, and point you to a valid location, then you could save that location in a setting in My.Settings, so that you would know where to look next time.
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by Shaggy Hiker View Post
    what you could do, rather than just give the messagebox saying the program isn't there, is give them the option of searching for the location. If they accept, and point you to a valid location, then you could save that location in a setting in My.Settings, so that you would know where to look next time.
    If I was good at programming I may have done exactly what you suggest but since I am very green in regards to programming, I feel the effort of trying to figure it out (even with help from this forum) is just not worth it. Instead I chose to include "View the 'Program List' page for program information and download." as part of the pop-up message. "Program List" is accessed via a button from the main console of my program. I felt the message should be enough to make sure the user has the program in the default folder and not in some other random location.

  16. #16
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by Shaggy Hiker View Post
    That may be a problem that isn't worth solving. If somebody goes and puts the application in some random location, then it really could be almost anywhere, including on a network drive. Searching the entire system looking for the file on the offhand chance that it exists is beyond reason. However, what you could do, rather than just give the messagebox saying the program isn't there, is give them the option of searching for the location. If they accept, and point you to a valid location, then you could save that location in a setting in My.Settings, so that you would know where to look next time.
    Actually that Settings file is what I would go with even for "remembering" whether it's in the Program Files or Program Files (x86) folders and whether it's speccy.exe or speccy64.exe.

    Basically, if the Settings file doesn't exist when the program runs that means it's a first time use and you could kick off a search subroutine to look for all the files in the various locations (like what he already has) and set a variable in the Settings file of it's location, then when the program runs again the settings file exists so it simply loads that information and doesn't need to take the time to search for it.
    I would also note that there should be a Form added to it that allows the user to view that info and change it as needed, like if they move or re-install one of the programs they can update the Settings location of it manually in your program. Also you can provide a button to have your program re-search for the programs on that form.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How do you code the execution of a file that goes by two names?

    Yeah, that's what I'd do, too.

    My.Settings is dead simple to learn, so don't be shying away from that one. However, once you get into it, there is a variety of things you can do. The simple steps are to go into Project | Properties, and go to the Settings tab. On that tab, you can set application settings (almost certainly what you want), or user settings. Just write some name for the setting, make it type string, and you can access it like a variable that is stored between runs of the computer with:

    My.Settings.YourSettingNameHere

    It's an easy way to store simple strings like this, or other simple types. However, you can get more complicated.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by JuggaloBrotha View Post
    I would also note that there should be a Form added to it that allows the user to view that info and change it as needed, like if they move or re-install one of the programs they can update the Settings location of it manually in your program.
    I would imagine that a similar form, could be used to enable a user to download any program he can think of (i.e. a program not linked to within my program) to any location he/she pleases and by entering the name of the program and it's location, the user can then access the program from within my program. I guess this would work if I somehow provide a series of buttons that the user can somehow assign to each personal program.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, that's what I'd do, too.

    My.Settings is dead simple to learn, so don't be shying away from that one. However, once you get into it, there is a variety of things you can do. The simple steps are to go into Project | Properties, and go to the Settings tab. On that tab, you can set application settings (almost certainly what you want), or user settings. Just write some name for the setting, make it type string, and you can access it like a variable that is stored between runs of the computer with:

    My.Settings.YourSettingNameHere

    It's an easy way to store simple strings like this, or other simple types. However, you can get more complicated.
    This would likely seem very easy and run-of-the-mill coding to someone with some programming experience. For me it would require quite some time and training to accomplish what you suggest.

    I truly envy you guys with all this coding knowledge and experience and I sincerely wish I had all your programming know how. I will look into it and see what I can come up with, but perhaps after I release the version I'm working on now. That version will be freeware. JuggaloBrotha and Shaggy Hiker, sometime in a future version, if I can somehow implement the ideas you both posted along with what I added above (a user downloading any program to any location) then perhaps that future version will have a modest price tag on it. I know one thing, many users would find it darn handy.

  20. #20
    Lively Member
    Join Date
    Jun 2017
    Posts
    77

    Re: How do you code the execution of a file that goes by two names?

    I would probably just search the registry and get the path to the executable. That way you could avoid hardcoding the path in your code.

    But yeah, probably it's an overkill in this situation

  21. #21
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by razz3333 View Post
    I would imagine that a similar form, could be used to enable a user to download any program he can think of (i.e. a program not linked to within my program) to any location he/she pleases and by entering the name of the program and it's location, the user can then access the program from within my program. I guess this would work if I somehow provide a series of buttons that the user can somehow assign to each personal program.
    Only have the buttons for one of the programs on the screen then use a grid, like the ListView (in details view), to display the list of programs and their locations (and any other info), then when a button is clicked it's a matter of using the grid's selected item to know which is being edited.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by BlackRiver1987 View Post
    But yeah, probably it's an overkill in this situation
    You may be right BlackRiver1987.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    75

    Re: How do you code the execution of a file that goes by two names?

    Quote Originally Posted by JuggaloBrotha View Post
    Only have the buttons for one of the programs on the screen then use a grid, like the ListView (in details view), to display the list of programs and their locations (and any other info), then when a button is clicked it's a matter of using the grid's selected item to know which is being edited.
    Sounds like a great idea. So is this about right: have a button titled something like "Program List" and then have a button on the Program List form, titled something like "Launch", which will launch the selected program in the list?

    Now I just have to try and figure this stuff out before I pass, being nearly 69 I figure that's about 16 years!

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