如何 运行 并行排列列表 + 从 .txt 文件获取输入?
How to run Array list in parallel + taking inputs from .txt file?
我有下面的程序,它工作正常,程序的功能是从数组中获取 IP 地址和主机名,然后通过 IP/host 名称的子字符串向下传递,然后它在远程机器上创建 .ZIP 文件 "using 3rd party library" 并将文件传输到中央服务器。我坚持的要求:
如何让我的程序从.txt文件中获取我的计算机列表而不是将其嵌入到程序中,所以如果需要添加新的机器IP我只需要添加它在 .txt 文件中
我如何使我的进程 运行 并行,因为我的程序将 运行 IP 一个接一个地串行 IP,这比一次执行所有进程需要更长的时间。
代码:
Imports System.IO
Module Module1
Dim RemoteComputer As New List(Of String)
Const FILENAME As String = "c:\temp\test.txt"
Sub Main()
Try
Dim reader As New StreamReader(FILENAME)
While reader.EndOfStream = False
RemoteComputer.Add(reader.ReadLine)
End While
For Each ComputerName In RemoteComputer
Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
Dim ZipToCreate As String = "\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
Using zip As ZipFile = New ZipFile
Dim filenames As String() = System.IO.Directory.GetFiles("\" & IPAddress & "\C$\Sample\")
For Each filename In filenames zip.AddFile(filename, "")
Next
zip.Save(ZipToCreate)
File.Copy("\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
Console.WriteLine("File copied successfully.....")
End Using
Else : Console.WriteLine("Remote machine is not reachable")
End If
Next
Catch ex As Exception
Console.Error.WriteLine(ex.Message.ToString())
Finally
Console.ReadLine()
End Try
End Sub
结束模块
我知道你不应该只是在 Whosebug 问题中转储代码,但这里有一个参考解决方案:
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Module Module1
Dim RemoteComputer As New List(Of String)
Const FILENAME As String = "c:\temp\test.txt"
Sub Main()
Dim reader As New StreamReader(FILENAME)
While reader.EndOfStream = False
RemoteComputer.Add(reader.ReadLine)
End While
Parallel.ForEach(Of String)(RemoteComputer,
Sub(ComputerName As String)
Try
Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
Dim ZipToCreate As String = "\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
Using zip As ZipFile = New ZipFile
Dim filenames As String() = System.IO.Directory.GetFiles("\" & IPAddress & "\C$\Sample\")
For Each file In filenames
zip.AddFile(file, "")
Next
zip.Save(ZipToCreate)
File.Copy("\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
Console.WriteLine("File copied successfully.....")
End Using
Else
Console.WriteLine("Remote machine is not reachable")
End If
Catch ex As Exception
Console.Error.WriteLine(ex.Message.ToString())
Finally
Console.ReadLine()
End Try
End Sub)
End Sub
End Module
我有下面的程序,它工作正常,程序的功能是从数组中获取 IP 地址和主机名,然后通过 IP/host 名称的子字符串向下传递,然后它在远程机器上创建 .ZIP 文件 "using 3rd party library" 并将文件传输到中央服务器。我坚持的要求:
如何让我的程序从.txt文件中获取我的计算机列表而不是将其嵌入到程序中,所以如果需要添加新的机器IP我只需要添加它在 .txt 文件中
我如何使我的进程 运行 并行,因为我的程序将 运行 IP 一个接一个地串行 IP,这比一次执行所有进程需要更长的时间。
代码:
Imports System.IO
Module Module1
Dim RemoteComputer As New List(Of String)
Const FILENAME As String = "c:\temp\test.txt"
Sub Main()
Try
Dim reader As New StreamReader(FILENAME)
While reader.EndOfStream = False
RemoteComputer.Add(reader.ReadLine)
End While
For Each ComputerName In RemoteComputer
Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
Dim ZipToCreate As String = "\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
Using zip As ZipFile = New ZipFile
Dim filenames As String() = System.IO.Directory.GetFiles("\" & IPAddress & "\C$\Sample\")
For Each filename In filenames zip.AddFile(filename, "")
Next
zip.Save(ZipToCreate)
File.Copy("\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
Console.WriteLine("File copied successfully.....")
End Using
Else : Console.WriteLine("Remote machine is not reachable")
End If
Next
Catch ex As Exception
Console.Error.WriteLine(ex.Message.ToString())
Finally
Console.ReadLine()
End Try
End Sub
结束模块
我知道你不应该只是在 Whosebug 问题中转储代码,但这里有一个参考解决方案:
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Module Module1
Dim RemoteComputer As New List(Of String)
Const FILENAME As String = "c:\temp\test.txt"
Sub Main()
Dim reader As New StreamReader(FILENAME)
While reader.EndOfStream = False
RemoteComputer.Add(reader.ReadLine)
End While
Parallel.ForEach(Of String)(RemoteComputer,
Sub(ComputerName As String)
Try
Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
Dim ZipToCreate As String = "\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
Using zip As ZipFile = New ZipFile
Dim filenames As String() = System.IO.Directory.GetFiles("\" & IPAddress & "\C$\Sample\")
For Each file In filenames
zip.AddFile(file, "")
Next
zip.Save(ZipToCreate)
File.Copy("\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
Console.WriteLine("File copied successfully.....")
End Using
Else
Console.WriteLine("Remote machine is not reachable")
End If
Catch ex As Exception
Console.Error.WriteLine(ex.Message.ToString())
Finally
Console.ReadLine()
End Try
End Sub)
End Sub
End Module