simpelt .net loop fejler
HejI den her kode sker der en fejl ved:
Dim tempProc As Process = Process.GetProcessesByName(SpywareArea.SelectedItems(i).Text)
Value of type '1-dimensional array of System.Diagnostics.Process' cannot be converted to 'System.Diagnostics.Process'.
Hvad betyder det ?
[souce]
Public Class Form1
Inherits System.Windows.Forms.Form
Dim EnTekst As String
#Region "ProcessList Funktionen"
Function getRunningProcessList(ByVal strComputer)
' Hvis intet computernavn bliver specifikeret kører programmet på den lokale maskine
If Len(strComputer) = 0 Then
strComputer = Environment.MachineName
End If
On Error Resume Next
Dim ProcessSet
' Henten listen af kørende processor fra Windows Management Instrumentation
ProcessSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2").ExecQuery("select * from Win32_Process")
If (Not Err.Number = 0) Then
getRunningProcessList = "Kunne ikke retunerer processlist for computer: " & strComputer & vbCrLf & vbCrLf & "Fejl " & Err.Number & " " & Err.Description
End If
' Laver tekst output til CheckedListBox
Dim strProcessList
Dim Process
Dim strPlist
Label1.Text = ""
SpywareArea.Items.Clear()
Label1.Text = "Processor kørende på computer: " & strComputer
For Each Process In ProcessSet
strProcessList = strProcessList & Process.Description & vbTab & vbTab
strPlist = Process.Description & vbTab & vbTab & Process.ExecutablePath
SpywareArea.Items().Add(strPlist, CheckState.Unchecked)
If Len(Process.Description) < 8 Then
strProcessList = strProcessList & vbTab
End If
strProcessList = strProcessList & Process.ExecutablePath & vbCrLf
Next
' Retunerer resultatet
getRunningProcessList = strProcessList
End Function
#End Region
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DeleteKnap As System.Windows.Forms.Button
Friend WithEvents SpywareArea As System.Windows.Forms.CheckedListBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ScanKnap As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.ScanKnap = New System.Windows.Forms.Button
Me.SpywareArea = New System.Windows.Forms.CheckedListBox
Me.DeleteKnap = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ScanKnap
'
Me.ScanKnap.Cursor = System.Windows.Forms.Cursors.Default
Me.ScanKnap.Location = New System.Drawing.Point(256, 416)
Me.ScanKnap.Name = "ScanKnap"
Me.ScanKnap.Size = New System.Drawing.Size(80, 24)
Me.ScanKnap.TabIndex = 2
Me.ScanKnap.Text = "Scan"
'
'SpywareArea
'
Me.SpywareArea.Location = New System.Drawing.Point(24, 32)
Me.SpywareArea.Name = "SpywareArea"
Me.SpywareArea.Size = New System.Drawing.Size(624, 364)
Me.SpywareArea.TabIndex = 3
'
'DeleteKnap
'
Me.DeleteKnap.Location = New System.Drawing.Point(72, 416)
Me.DeleteKnap.Name = "DeleteKnap"
Me.DeleteKnap.Size = New System.Drawing.Size(120, 24)
Me.DeleteKnap.TabIndex = 4
Me.DeleteKnap.Text = "Delete Checked"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(480, 16)
Me.Label1.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(664, 478)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.DeleteKnap)
Me.Controls.Add(Me.SpywareArea)
Me.Controls.Add(Me.ScanKnap)
Me.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "UnsafeRemover"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub DeleteKnap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteKnap.Click
Dim i As Integer
Dim a As Integer
On Error Resume Next
i = 0
a = SpywareArea.CheckedItems.Count
While i <= a
If SpywareArea.CheckedItems(i).Checked = True Then
SpywareArea.Items.Remove(SpywareArea.CheckedItems(i))
Dim tempProc As Process = Process.GetProcessesByName(SpywareArea.SelectedItems(i).Text)
tempProc.CloseMainWindow()
tempProc.WaitForExit()
getRunningProcessList("")
i = i + 1
End If
End While
End Sub
Private Sub ScanKnap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScanKnap.Click
getRunningProcessList("")
End Sub
End Class
[/souce]
