• C#循环读取文件流,按行读取


    public Dictionary<string,string> GetSourceDisksElements(String section)
    {
            section = "[" + section;
            Dictionary<string, string> keyToValue = new Dictionary<string, string>();
            //打开文件流,开始读取文件
            using (StreamReader sin = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                    for (String str = sin.ReadLine(); str != null; str = sin.ReadLine())
                    {
                        if (str.Trim().StartsWith(section, StringComparison.OrdinalIgnoreCase)) //some section has comment. 
                        {
                            for (str = sin.ReadLine(); str != null && !str.Trim().StartsWith("["); str = sin.ReadLine()) //loop for get string, until to next section or end of file.
                            {
                                if (!String.IsNullOrEmpty(str.Trim()) && !str.Trim().StartsWith(";"))  //drop the comment line and empty line. //去掉name-value对的限制,因为存在无=的情况。20100309.str.Contains('=') &&  only get the name-value pair line.
                                {   
                                    String[] keyValues = str.Split('=');
                                    if (section.Equals("[SourceDisksNames"))
                                    {
                                        string[] noCommentValues = keyValues[1].Split(';');
                                        string realValue = noCommentValues[0].Trim();
    
                                        String[] sourceDiskCfg = realValue.Split(',');  //disk-description[,[tag-or-cab-file],[unused],[path],[flags][,tag-file]]  (tag-file : Windows XP and later versions of Windows)
                                        String diskpath = String.Empty;
                                        if (sourceDiskCfg.Length > 3)
                                            diskpath = sourceDiskCfg[3].Trim();
    
                                        if (diskpath.StartsWith("""))
                                            diskpath = RemoveQuotes(diskpath);
    
                                        if (!String.IsNullOrEmpty(diskpath) && diskpath.StartsWith("."))
                                            diskpath = diskpath.Substring(diskpath.IndexOf('\'));
    
                                        //20150112
                                        if (!String.IsNullOrEmpty(diskpath) && !diskpath.StartsWith("\"))
                                            diskpath = "\" + diskpath;
    
                
                                        keyToValue.Add(keyValues[0].Trim(), diskpath);
                                    }
                                    else
                                    {
                          
                                        string[] noCommentValues = keyValues[1].Split(';');
                                        string realValue = noCommentValues[0].Trim();
                                        keyToValue.Add(keyValues[0].Trim(), realValue);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                return keyToValue;
            }
    View Code
  • 相关阅读:
    SpringBoot2.x前后端分离跨域问题及Swagger不能访问
    SpirngBoot2.x整合Swagger2接口文档
    SpringBoot2.x整合Druid数据源
    SpringBoot2.x整合logback 实现自动打印日志
    docker 进入 mysql中的操作
    Intellij Springboot (子模块)访问jsp页面404
    运行rabbitmq 的docker
    mybatis拦截器修改sql重新set后不生效?
    oracle+mybatis如何在新增时返回主键(自增序列)的值?
    oracle+mybatis报“未找到要求的from关键字”错误?
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4220723.html
Copyright © 2020-2023  润新知