locked
Closing application on a different computer on the same network RRS feed

  • Question

  • I need to close an application running on a computer in the same workgroup using VBE.

     

    I know how to do it when the application is running on the same computer.

     

    My reading of the help section leads me to conclude that this is not possible.

     

    Can anybody tell me if this can be done and how?

     

    Thanks,

     

    Antonio

    Monday, March 27, 2006 9:38 PM

Answers

  • Hi Antonio,

    I just wanted to let you know that I got through the part that I was trying to get through concerning sharing a data file on a mapped drive without other users over writting old data.  Below is the code.  The form has 80 combobox, 80 textbox, 1 button and 1 timer.  Timer is set to 30000 ms (30 seconds).  The program will load the data from the data file, which is text, and make all the controls except the button visible on the load and button1 click events.  On the timer event the the program will hide all controls except for the button.  The combobox's and the textbox's are have the tags listed from 1-160.  I hope this helps anybody who wants to know.  Special thanks to all who have helped me with this project.

    Public Class Form1

    Dim mystamp2 As Date

    Dim mystamp As Date

    Dim _numberOfTextboxes As Integer = 160

    Dim _text(_numberOfTextboxes - 1) As String

    Dim _CommaText As String

    Dim _control As Control

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    If Button1.Visible = False Then

    mystamp2 = FileDateTime("bay.txt")

    If mystamp >= mystamp2 Then

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    'Join the array into one comma delimited string

    'Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    'Save the file, I am using .net 2005. If you are using an

    'earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("bay.txt", _CommaText, False)

    ElseIf mystamp < mystamp2 Then

    MsgBox("Data not saved, File was modified since you opened it. Please reopen the program and enter your data.")

    End If

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Button1.Visible = False

    mystamp = FileDateTime("bay.txt")

    Dim filename As String = ("bay.txt")

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    filename = My.Computer.FileSystem.ReadAllText(filename, encoding)

    Dim TestArray() As String = Split(filename, "®")

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    Next

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    If Button1.Visible = False Then

    mystamp2 = FileDateTime("bay.txt")

    If mystamp >= mystamp2 Then

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    ' Join the array into one comma delimited string

    ' Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    ' Save the file, I am using .net 2005. If you are using an

    ' earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("bay.txt", _CommaText, False)

    ElseIf mystamp < mystamp2 Then

    MsgBox("Data not saved, File was modified since you opened it. Please reopen the program and enter your data.")

    End If

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    _control.Visible = False

    End If

    If TypeOf _control Is ComboBox Then

    _control.Visible = False

    End If

    If TypeOf _control Is Label Then

    _control.Visible = False

    End If

    Next

    Button1.Visible = True

    End If

    'Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    mystamp = FileDateTime("bay.txt")

    Dim filename As String = ("bay.txt")

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    filename = My.Computer.FileSystem.ReadAllText(filename, encoding)

    Dim TestArray() As String = Split(filename, "®")

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    Next

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    _control.Visible = True

    End If

    If TypeOf _control Is ComboBox Then

    _control.Visible = True

    End If

    If TypeOf _control Is Label Then

    _control.Visible = True

    End If

    Next

    Button1.Visible = False

    End Sub

    End Class

    Wednesday, March 29, 2006 1:48 AM

All replies

  • I'm guessing that it is not possible to close programs on a different computer do to security issues.

    You might be able to make a program that will run on the computer that you want to close the program on.  I don't know the code to make a program close.

    Here's my plan:

    Program 1 (for the computer that you control the other computers program with):

    create a txt file called close.txt and place it on a maped drive I'll call x so the path to the text file will be x:\close.txt

    make an invisible textbox control that is textbox1 and make textbox1.tag = "1"

    make a button on a windows form to make the action happen.

    ' on the form:

    Dim _numberOfTextboxes As Integer = 1

    Dim _text(_numberOfTextboxes - 1) As String

    Dim _CommaText As String

    Dim _control As Control

    ' on the button1 event click:

    textbox1.text  =  "closeapp1"

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    ' Join the array into one comma delimited string

    ' Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    ' Save the file, I am using .net 2005. If you are using an

    ' earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("X:\close.txt", _CommaText, False)

     

    Program 2 (for the computer you want to close the program on):

    make form with 1 textbox invisible and tag it 1

    ' on the form:

    Dim _numberOfTextboxes As Integer = 1

    Dim _text(_numberOfTextboxes - 1) As String

    Dim _CommaText As String

    Dim _control As Control

    ' You need some type of timer to execute this code every 15 secs, or what you think is appropriate.

     ' on the timer event:

    Dim filename As String = ("X:\close.txt")

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    filename = My.Computer.FileSystem.ReadAllText(filename, encoding)

    Dim TestArray() As String = Split(filename, "®")

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    Next

    if textbox1.text = "closeapp1" then

    ' Your app closing code here

    textbox1.text = " "

    next

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    ' Join the array into one comma delimited string

    ' Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    ' Save the file, I am using .net 2005. If you are using an

    ' earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("X:\close.txt", _CommaText, False)

    end if

     

    ' might be an easier way but I think this will work.

    Bryan

    www.tekpropc.com

    Monday, March 27, 2006 11:01 PM
  • Hy Bryan,

     

    Thank you for your email and extensive work. It requires some time to digest, I will look into it in the next few days. Regards, Antonio

     

    Tuesday, March 28, 2006 8:05 AM
  • NP,

    If you wouldn't mind I'd like to get the completed code for it for a project of mine.  Mainly the timer you use.  I'm trying to get a program to open from a mapped network drive and share the same data file without different users on the network writting outdated data over new data file.

    Good luck, Bryan.

    Tuesday, March 28, 2006 8:10 AM
  • Hi Bryan,

    Please do let me know of your progress, I have a similar interest.

    This is the code I use to close Outlook in the same computer. No timer:

     

    Dim processes() As Process = Process.GetProcesses

    For Each proc As Process In processes

    If proc.Id > 4 Then

    If proc.MainModule.ModuleName = "OUTLOOK.EXE" Then proc.CloseMainWindow()

    If proc.MainModule.ModuleName = "OUTLOOK.EXE" Then proc.Close()

    End If

    Next

    Tuesday, March 28, 2006 8:19 AM
  • Hi Antonio,

    I just wanted to let you know that I got through the part that I was trying to get through concerning sharing a data file on a mapped drive without other users over writting old data.  Below is the code.  The form has 80 combobox, 80 textbox, 1 button and 1 timer.  Timer is set to 30000 ms (30 seconds).  The program will load the data from the data file, which is text, and make all the controls except the button visible on the load and button1 click events.  On the timer event the the program will hide all controls except for the button.  The combobox's and the textbox's are have the tags listed from 1-160.  I hope this helps anybody who wants to know.  Special thanks to all who have helped me with this project.

    Public Class Form1

    Dim mystamp2 As Date

    Dim mystamp As Date

    Dim _numberOfTextboxes As Integer = 160

    Dim _text(_numberOfTextboxes - 1) As String

    Dim _CommaText As String

    Dim _control As Control

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    If Button1.Visible = False Then

    mystamp2 = FileDateTime("bay.txt")

    If mystamp >= mystamp2 Then

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    'Join the array into one comma delimited string

    'Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    'Save the file, I am using .net 2005. If you are using an

    'earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("bay.txt", _CommaText, False)

    ElseIf mystamp < mystamp2 Then

    MsgBox("Data not saved, File was modified since you opened it. Please reopen the program and enter your data.")

    End If

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Button1.Visible = False

    mystamp = FileDateTime("bay.txt")

    Dim filename As String = ("bay.txt")

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    filename = My.Computer.FileSystem.ReadAllText(filename, encoding)

    Dim TestArray() As String = Split(filename, "®")

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    Next

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    If Button1.Visible = False Then

    mystamp2 = FileDateTime("bay.txt")

    If mystamp >= mystamp2 Then

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _text(_index - 1) = _control.Text

    End If

    Next

    ' Join the array into one comma delimited string

    ' Alt 0174 is ® and can't be typed

    _CommaText = Join(_text, "®"c)

    ' Save the file, I am using .net 2005. If you are using an

    ' earlier version you will have to use your method.

    My.Computer.FileSystem.WriteAllText("bay.txt", _CommaText, False)

    ElseIf mystamp < mystamp2 Then

    MsgBox("Data not saved, File was modified since you opened it. Please reopen the program and enter your data.")

    End If

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    _control.Visible = False

    End If

    If TypeOf _control Is ComboBox Then

    _control.Visible = False

    End If

    If TypeOf _control Is Label Then

    _control.Visible = False

    End If

    Next

    Button1.Visible = True

    End If

    'Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    mystamp = FileDateTime("bay.txt")

    Dim filename As String = ("bay.txt")

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    filename = My.Computer.FileSystem.ReadAllText(filename, encoding)

    Dim TestArray() As String = Split(filename, "®")

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    If TypeOf _control Is ComboBox Then

    Dim _index As Integer = CInt(_control.Tag)

    _control.Text = TestArray(_index - 1)

    End If

    Next

    For Each _control In Me.Controls

    If TypeOf _control Is TextBox Then

    _control.Visible = True

    End If

    If TypeOf _control Is ComboBox Then

    _control.Visible = True

    End If

    If TypeOf _control Is Label Then

    _control.Visible = True

    End If

    Next

    Button1.Visible = False

    End Sub

    End Class

    Wednesday, March 29, 2006 1:48 AM