C#读取记事本,里面有600万条数据,放入数组时:System.OutOfMemoryException

发布时间 2023-10-28 10:47:07作者: 溜溜球_小钢wan

 

原因:使用文件流,然后读取文件内容,再解析的时候,会报内存溢出

 

处理办法:使用/n分隔

        /// <summary>
        /// 通过记事本,获取CRM所有客户的某个字段
        /// </summary>
        /// <returns></returns>
        public static List<string> GetFieldByText(string fieldname)
        {
            try
            {
                string filePath = string.Empty;
                string debugPath = System.Environment.CurrentDirectory;// Directory.GetCurrentDirectory();


                filePath = debugPath + $"\\CrmData\\{fieldname}.txt";

                int intNoOfLines = 0;
                using (StreamReader oReader = new
                StreamReader(filePath))
                {
                    while (oReader.ReadLine() != null) intNoOfLines++;
                }
                string[] strArrLines = new string[intNoOfLines];
                int intIndex = 0;
                using (StreamReader oReader = new
                StreamReader(filePath))
                {
                    string strLine;
                    while ((strLine = oReader.ReadLine()) != null)
                    {
                        strArrLines[intIndex++] = strLine;
                    }
                }

                #region xxxxx下面的方式会超出内存xxxxxxxxxxxxxx
                //if (!File.Exists(filePath))
                //{
                //    return new string[] { };
                //}
                //StreamReader streamReader = new StreamReader(filePath, Encoding.GetEncoding("GB2312"));
                //string content = streamReader.ReadToEnd();
                //streamReader.Close();

                //string[] textList = content.Split('&');  //内存溢出
                #endregion

                return strArrLines.ToList();

            }
            catch (Exception ex)
            {
                LoggerHelper.Log("CDPDataHelperError", "报错:" + ex.Message);
                throw;
            }
        }

 

发现的源地址:https://www.codenong.com/13415916/