C#TXTファイルの読み取りと書き込み


    /// 
        ///      
        /// 
        ///     
        ///     
        ///        
        /// 
        public static KeyValuePair CreateFile(string path, string name, string contents)
        {
            name = path + "\\" + name;
            var pair = new KeyValuePair(true, name);

            //        
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                FileStream fs;
                //        ,        ,    ,     
                if (File.Exists(name))
                {
                    ClearTxt(name);
                    fs = new FileStream(name, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(name, FileMode.Create, FileAccess.Write);
                }

                //          ANSI,   utf-8 
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.Write(contents);
                    sw.Close();
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                pair = new KeyValuePair(false, ex.Message);
            }
            return pair;
        }

        /// 
        ///       
        /// 
        /// 
        public static void ClearTxt(string txtPath)
        {
            using (var stream = File.Open(txtPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.SetLength(0);

                stream.Close();
            }
        }
        /// 
        ///      
        /// 
        ///        
        /// 
        public static KeyValuePair> ReadFile(string path)
        {
            var pair = new KeyValuePair>();
            var str = new List();
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        while (!sr.EndOfStream)
                        {
                            string sLine = sr.ReadLine();
                            if (sLine.Length < 1)
                            {
                                continue;
                            }
                            str.Add(sLine);
                        }
                    }
                }
                if (str.Contains(""))
                {
                    str.Add("         ");
                    pair = new KeyValuePair>(false, str);
                }
                else
                {
                    pair = new KeyValuePair>(true, str);
                }
            }
            catch (Exception ex)
            {
                str.Add(ex.Message);
                pair = new KeyValuePair>(false, str);
            }
            return pair;
        }

呼び出し例:
読み込み:basCommon.ReadFile(「http://*****パス/info.txt」)
書き込み:basCommon.CreateFile(「http://*****パス」,「ファイル名.txt」,ファイル内容);