简体   繁体   中英

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.

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.

$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]

Created a $reader object and pointed it to the 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.

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

2. Using [System.Net.WebRequest]

Created a $reader object and pointed it to the 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.

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

3. Using [System.Net.WebClient]

I thought maybe I could create a WebClient and point it to the URI as shown here

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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