[Help] file open check

Posts 1–15 of 15 · Page 1 of 1
[Help] file open check
ok im new to VB i made a few small projects and im working on a program to open all my texture modding stuff for mw2 its a simple press the button open the corresponding program/folder

my first question is tho is there a way to have a text box display if the file/program is open or not

my other question is, is there a way to stop it from opening new instances of the file or program and just bring them to front

any help will be appreciated
Thanks in advance
Yes it's possible. This is a way I did it a while ago (I'm not sure if it's the best method but it works)

[php]
Private Function IsFileOpen(ByVal filepath As String) As Boolean

Try

IO.File.Open((filepath), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)

Return True

Catch ex As Exception

Return False

End Try

End Function
[/php]

As for bringing the program to the front if it's already open:

You'll need this declaration:

[php]
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
[/php]

Then you'll need to first make sure that the program is open

[php]

Private Function IsProcessOpen(ByVal processName As String) As boolean

Dim theProc as Process() = Process.GetProcessesByName(processName)

If theProc.Length > 0 then
Return True
Else
Return False
End If

End Function

[/php]

Then to bring it to front.

[php]

If IsProcessOpen("Engine") then

For Each p As Process In Process.GetProcesses(My.Computer.Name)
If p.ProcessName = "Engine" Then
SetForeGroundWindow(p.MainWindowHandle)
End If
Next

End if

[/php]

Something like that. Did i miss anything?
ok the bringing prossess to front worked out fine im not sure about where to put the first code tho this is what i have so far

Edit: ok didnt even need to bring to front code lol only 1 that open a new instance is crcfixer and it does it because there is no minimize option so no matter what it opens another so that part or the code was removed
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\HxD")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\Adobe_Photoshop_CS5\Adobe Photoshop CS5\Photoshop")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\CRC32 Fixer\CRCFixer")
    End Sub
End Class
Quote Originally Posted by Maniac101 View Post
ok the bringing prossess to front worked out fine im not sure about where to put the first code tho this is what i have so far

Edit: ok didnt even need to bring to front code lol only 1 that open a new instance is crcfixer and it does it because there is no minimize option so no matter what it opens another so that part or the code was removed
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\HxD")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\Adobe_Photoshop_CS5\Adobe Photoshop CS5\Photoshop")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\CRC32 Fixer\CRCFixer")
    End Sub
End Class
Umm what did you want exactly?
i wanted to have a text box display is the file or program is open or not
like if i click the pakscape button the text box would display open or w/e
Well, first of all, whack this anywhere in your code:

[php]
Private Function IsFileInUse(ByVal filepath As String) As Boolean

Try

IO.File.Open((filepath), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
Return True

Catch ex As Exception

Return False

End Try

End Function
[/php]

Then in a timer a timer event or whatever:

[php]
Dim PathToCheck As String = ""D:\MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape"

If IsFileInUse(PathToCheck) Then
TextBox1.Text = "File is currently in use"
Else
TextBox1.Text = "File is not being used at this moment"
End if
[/php]

You may or may not need the file extension for PakScape (i.e .mp3, .rez or whatever)
ok we are close and i thank you for the help i know it gets old helping nubs but now it says file is not in use at the moment is there a way so when i open it it changes because its not changing thanks again
Quote Originally Posted by Maniac101 View Post
ok we are close and i thank you for the help i know it gets old helping nubs but now it says file is not in use at the moment is there a way so when i open it it changes because its not changing thanks again
Did you put that code into a timer? (The "IsFileOpen(PathToCheck)")
yes i did but it just does not wanna change
Code:
Public Class Form1

    Private Function IsFileInUse(ByVal filepath As String) As Boolean

        Try

            IO.File.Open((filepath), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
            Return True

        Catch ex As Exception

            Return False

        End Try

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\HxD")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\Adobe_Photoshop_CS5\Adobe Photoshop CS5\Photoshop")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        System.Diagnostics.Process.Start("D:MW2 Stuff\texture mods\Modding folder\CRC32 Fixer\CRCFixer")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim PathToCheck As String = "D:\MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape"
        If IsFileInUse(PathToCheck) Then
            TextBox1.Text = "File is currently in use"
        Else
            TextBox1.Text = "File is not being used at this moment"
        End If
    End Sub
End Class
Okay there's a few steps from here

1. Double check that the timer is enabled
2. Add the file extension to "D:\MW2 Stuff\texture mods\Programs and tools\PakScape\PakScape"
3. Double check that the path is correct in the first place.
4. Make sure the file you're testing to see whether it's open is actually the one you wanna check.

If you've done all of them, give me a yell and I can TV you for a bit and try work it out.
ok if i add the file extension and debug it says "The process cannot access the file because it is being used by another process"
Quote Originally Posted by Maniac101 View Post
ok if i add the file extension and debug it says "The process cannot access the file because it is being used by another process"
Do you have TV? I think this will go a lot faster with TV assistance. PM your TV ID/PW and I'll try give you a hand.
thanks for the help J-Deezy
but we got off topic and forgot 1 part
could anyone tell me how to check for an open window name and report back?
like the external hacks for mw2 do i need to know if ...folder is open instead of ...process.
Quote Originally Posted by Maniac101 View Post
thanks for the help J-Deezy
but we got off topic and forgot 1 part
could anyone tell me how to check for an open window name and report back?
like the external hacks for mw2 do i need to know if ...folder is open instead of ...process.
Oh yeah lol, sorry about that guess I got a little carried away.

Put this just below "Public Class Form1"

[php]
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
[/php]

Now add this to wherever you wish to detect whether it's open or not.

[php]
Dim wHandle As Long = FindWindow(vbNullString, "Untitled - Notepad") 'change "Untitled - Notepad" to the window name of whatever you want to check, it must be EXACTLY as it appears in the bottom bar of your screen.'

If wHandle > 0 then
'Do whatever you want if it's open'
Else
'Do whatever you want if it isn't.'
End If
[/php]
If isprocessopen("processname without .exe") then
msgbox("Open!")
end if
Posts 1–15 of 15 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us