csvhelper引发了错误“无法访问处置对象”。从FTP解析CSV文件时

发布于 2025-01-29 03:50:32 字数 3285 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文