csvhelper引发了错误“无法访问处置对象”。从FTP解析CSV文件时
我正在尝试使用csvhelper解析.csv
文件。如果我尝试将数据解析为FTP路径中的动态对象,我会发现一个错误
无法访问处置对象。对象名称:'System.net.sockets.networkStream'。
这是我的代码
private WebRequest CreateRequest(string method)
{
WebRequest req = null;
try
{
req = WebRequest.Create(_host + folderpath);
req.Method = method;
req.Credentials = _credentials;
}
catch(Exception ex)
{
}
return req;
}
public void ReadCSVData(string filename)
{
List<string> myList = new List<string>();
folderpath = folderpath + "/" + filename;
var request = CreateRequest(WebRequestMethods.Ftp.DownloadFile);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csvReader.GetRecords<dynamic>().ToList();
}
}
}
为错误的stackTrace:
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)
at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
at CsvHelper.CsvParser.Read()
at CsvHelper.CsvReader.Read()
at CsvHelper.CsvReader.<GetRecords>d__87`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at FleetcheckDownLoad.FTP.ReadCSVData(String filename) in C:\FleetcheckDownLoad\FleetcheckDownLoad\FTP.cs:line 188
at FleetcheckDownLoad.DownLoad.dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e) in C:\FleetcheckDownLoad\FleetcheckDownLoad\DownLoad.cs:line 58
at System.Windows.Forms.DataGridView.OnCellDoubleClick(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnDoubleClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
I am trying to parse a .csv
file using CsvHelper. If I try to parse the data into dynamic object from ftp path, I get an error
Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.
Here is my code
private WebRequest CreateRequest(string method)
{
WebRequest req = null;
try
{
req = WebRequest.Create(_host + folderpath);
req.Method = method;
req.Credentials = _credentials;
}
catch(Exception ex)
{
}
return req;
}
public void ReadCSVData(string filename)
{
List<string> myList = new List<string>();
folderpath = folderpath + "/" + filename;
var request = CreateRequest(WebRequestMethods.Ftp.DownloadFile);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csvReader.GetRecords<dynamic>().ToList();
}
}
}
The stacktrace for the error:
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)
at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
at CsvHelper.CsvParser.Read()
at CsvHelper.CsvReader.Read()
at CsvHelper.CsvReader.<GetRecords>d__87`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at FleetcheckDownLoad.FTP.ReadCSVData(String filename) in C:\FleetcheckDownLoad\FleetcheckDownLoad\FTP.cs:line 188
at FleetcheckDownLoad.DownLoad.dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e) in C:\FleetcheckDownLoad\FleetcheckDownLoad\DownLoad.cs:line 58
at System.Windows.Forms.DataGridView.OnCellDoubleClick(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnDoubleClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论