c#存储或读取参数到文本文件函数

@存储或读取参数到文本文件函数

    public static bool SaveParamToFile(string logstring, string fileName,string sFilePath = "")
    {
        try
        {
            string path = "";
            if (sFilePath == "")
            {
                path = AppDomain.CurrentDomain.BaseDirectory + fileName;
            }
            else
            {
                path = sFilePath + fileName;
            }
            
            
            //写入参数
            using (StreamWriter writer = new StreamWriter(path, false))
            {
                writer.WriteLine(logstring);
            }
            return true;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return false;
        }
    }

    public static bool ReadParamFromFile(ref string readString, string fileName, string sFilePath = "")
    {
        try
        {
            string path = "";
            if (sFilePath == "")
            {
                path = AppDomain.CurrentDomain.BaseDirectory + fileName;
            }
            else
            {
                path = sFilePath + fileName;
            }

            //判断文件是否存在,没有则退出。
            if (!System.IO.File.Exists(path))
            {
                return false;
            }
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    if (sr.EndOfStream)
                    {
                        return false;
                    }
                    readString = sr.ReadLine();
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43232671/article/details/88883553