简体   繁体   English

如何在 PowerShell 中逐行使用 C# 从 URI 读取文件?

[英]How to use read file from URI using C# in PowerShell line by line?

Am trying to incorporate some C# code into my PowerShell, as I am still learning C# and getting a foothold on learning a new language.我正在尝试将一些 C# 代码合并到我的 PowerShell 中,因为我仍在学习 C# 并在学习新语言方面站稳脚跟。

I am able to open a file locally on my computer and read it line by line.我可以在我的计算机上本地打开一个文件并逐行读取它。 But cannot seem to work out how to open a file from the internet and parse it line by line.但似乎无法解决如何从互联网打开文件并逐行解析它。

This works to read the file line by line, and allows me to take action on the line items in the loop.这可以逐行读取文件,并允许我对循环中的行项目采取行动。

$f = 'C:\tmp\tinyUF.txt'
$reader = [System.IO.File]::OpenText($f)
while (-not($reader.EndOfStream)) {
    $line = $reader.ReadLine()
}
$x.Close()

This doesn't work, so I did some research and found that [System.IO.File] is not going to be able to read $f as a feed.这不起作用,所以我做了一些研究,发现[System.IO.File]无法读取$f作为提要。

$f = (Invoke-WebRequest -Uri https://algs4.cs.princeton.edu/15uf/tinyUF.txt).Content
$reader = [System.IO.File]::OpenText($f)
while (-not($reader.EndOfStream)) {
    $line = $reader.ReadLine()
}
$x.Close()

So I looked at different ways to solve my own problem and learn along the way:所以我研究了不同的方法来解决我自己的问题并在此过程中学习:

1. Using [System.Net.WebRequest] 1.使用[System.Net.WebRequest]

Created a $reader object and pointed it to the URL.创建了一个$reader object 并将其指向 URL。 But I can't see a method that allows me to read the data.但我看不到允许我读取数据的方法。 I was hoping it would work like Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content and I would be able to parse the data, but that didn't work out.我希望它会像Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content那样工作,我将能够解析数据,但这不起作用出去。

$reader = [System.Net.WebRequest]::Create('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')

2. Using [System.Net.WebRequest] 2.使用[System.Net.WebRequest]

Created a $reader object and pointed it to the URL.创建了一个$reader object 并将其指向 URL。 But I can't see a method that allows me to read the data.但我看不到允许我读取数据的方法。 I was hoping it would work like Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content and I would be able to parse the data, but that didn't work out.我希望它会像Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content那样工作,我将能够解析数据,但这不起作用出去。

$reader = [System.Net.WebRequest]::Create('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')

3. Using [System.Net.WebClient] 3.使用[System.Net.WebClient]

I thought maybe I could create a WebClient and point it to the URI as shown here我想也许我可以创建一个 WebClient 并将其指向 URI,如下所示

So I put that together, and got the error on the second line:所以我把它放在一起,并在第二行得到错误:

$reader = [System.Net.WebClient]::new('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')
MethodException: Cannot find an overload for "new" and the argument count: "1".

I tried some of the other solutions listed there, but didn't quite understand them.我尝试了那里列出的其他一些解决方案,但不太了解它们。

Actually, I found a working solution.实际上,我找到了一个可行的解决方案。

I was re-reading and testing before posting this question, I came up with a method to solve my own predicament.在发布这个问题之前,我正在重新阅读和测试,我想出了一个解决我自己困境的方法。 In the event anyone else is interested, here is the solution:如果其他人有兴趣,这里是解决方案:

# Set $f to point to the target file on the web
$f = 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt'

# $wc to represent the WebClient
$wc = [System.Net.WebClient]::new()

# Create a stream object, using the .OpenRead method on $f
$stream = $wc.OpenRead($f)

# Create a reader of the stream using System.IO.StreamReader
$reader = [System.IO.StreamReader]($stream)

# ForEach-Object didn't play nice, so using while until it displays the last line of the file
while (-not($reader.EndOfStream)) {
    $line = $reader.ReadLine();
    $line
}

# Close the StreamReader
$reader.Close()

# Dispose of the WebClient
$wc.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM