Skip to content

Instantly share code, notes, and snippets.

@lfgrando
Created October 5, 2018 14:13
Show Gist options
  • Save lfgrando/5e2ed1484aa6523d27962ce279fc6019 to your computer and use it in GitHub Desktop.
Save lfgrando/5e2ed1484aa6523d27962ce279fc6019 to your computer and use it in GitHub Desktop.
public static TextFieldParser GetTextFieldParser(string source)
{
if (!source.IsUrl())
return new TextFieldParser(source);
var file = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "files", $"{DateTime.Now:yyyyMMddHHmmssfff}", Path.GetFileName(source));
var path = Path.GetDirectoryName(file);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
// Save to file before calling the parser
using (var response = _httpClient.GetAsync(source).GetAwaiter().GetResult())
{
response.EnsureSuccessStatusCode();
using (var stream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult())
using (var fileStream = File.Create(file))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
return new TextFieldParser(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment