读文本信息


public bool UserIDfromFile(string userID)
    {
        bool result = false;
        string strFileName = HttpContext.Current.Server.MapPath(@".file") + @"userInfo.txt";
        if (!System.IO.File.Exists(strFileName))
        {
            throw new Exception(strFileName + "打开失败!");
            result = false;
        }
        try
        {
            string strFileContents = string.Empty;
            FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fs, System.Text.Encoding.Default);
            strFileContents = reader.ReadToEnd();
            strFileContents.Replace(" ", "");
            string[] Items = strFileContents.Trim().Split('=');
            if (Items.Length >= 2)
            {
                string userinfo = strFileContents.Split('=')[1].Trim();
                string[] userIDs = userinfo.Split(',');
                for (int i = 0; i < userIDs.Length; i++)
                {
                    if (userID == userIDs[i].Trim())
                    {
                        result = true;
                    }
                }
            }
            fs.Close();
            reader.Close();
        }
        catch
        {
            result = false;
        }
        return result;
    }

原文地址:https://www.cnblogs.com/libbybyron/p/4125880.html