本地文件夹添加删除

类1:定义一个发送的模板类型,其中有表单Id,文本框的值,对应的类型

 public class SendTemp
    {
        public SendTemp()
        {
        }
        public SendTemp(SendTemp temp)
        {
            this.FormId = temp.FormId;
            this.SendTypeId = temp.SendTypeId;
            this.TextValue = temp.TextValue;
        }
        public string TextValue
        {
            set;
            get;
        }
        public int FormId
        {
            get;
            set;
        }
        public int SendTypeId
        {
            get;
            set;
        }
    }

类2:定义要保存本地文件夹名称及文件名称和对应文件里面的所有元素

 public class SendTemplate
    {
        public SendTemplate() { }

        public SendTemplate(SendTemplate temp)
        {
            this.FileName = temp.FileName;
            List<SendTemp> list = new List<SendTemp>();
            temp.TemplateList.ForEach(t => { list.Add(new SendTemp(t)); });
            this.TemplateList = list;
        }

        public string FileName
        {
            set;
            get;
        }

        public string Name
        {

            //获取文件名称
            get { return FileName.Substring(FileName.LastIndexOf("\\") + 1); }
        }

        public string DirectoryName
        {
            get
            {
                //获取文件夹的名称
                int n = FileName.LastIndexOf("\\");
                int n2 = FileName.Substring(0, n).LastIndexOf("\\");
                return FileName.Substring(n2 + 1, n - n2 - 1);
            }
        }

        public List<SendTemp> TemplateList
        {
            set;
            get;
        }
    }

类3:对本地文本进行操作

public   class TManager
    {
        public static Dictionary<string, Dictionary<string, SendTemplate>> Templates;//存放文件夹及文件夹下面的文件
        //存放的本地路径
        public static string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "FolderName";//文件存放的路径

        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="connent">内容</param>
        /// <returns>是否成功</returns>
        public static bool UpdateXX(string path, string content)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(content);
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            return true;
        }
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="path1">文件夹名称</param>
        /// <returns></returns>
        public static bool DelDirectory(string floderName)
        {
            try
            {
                Directory.Delete(TManager.path + "\\" + floderName, true);//删除文件夹及其目录下的所有文件
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            return true;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName">文件</param>
        /// <returns></returns>
        public static bool DelFile(string fileName)
        {
            try
            {
                File.Delete(TManager.path + "\\" + fileName);//删除文件
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            return true;
        }

        /// <summary>
        /// 写入集合
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="listStr">集合</param>
        /// <returns>是否成功</returns>
        public static bool UpdateList(string path, Dictionary<string, List<string>> listStr)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                listStr.Keys.ToList().ForEach(k =>
                {
                    listStr[k].ForEach(s =>
                    {
                        sw.WriteLine(k + "\t" + s);
                    });
                });
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取文件夹与文件
        /// </summary>
        /// <returns>返回数据集合</returns>
        public static Dictionary<string, Dictionary<string, SendTemplate>> TemplateInit()
        {
            //定义返回泛型集合

            if (Directory.Exists(path))//判断路径是否存在
            {
                string nodeName;
                string nodeName2;
                Dictionary<string, SendTemplate> dalite;
                SendTemplate newArticles;
                foreach (string f in Directory.GetFileSystemEntries(path))
                {
                    int n = f.LastIndexOf("\\") + 1;
                    nodeName = f.Substring(n, f.Length - n);
                    if (f.Contains("."))
                    {
                        //Console.WriteLine(nodeName + "非目录");
                        continue;
                    }
                    dalite = new Dictionary<string, SendTemplate>();
                    foreach (string f2 in Directory.GetFileSystemEntries(f))
                    {
                        n = f2.LastIndexOf("\\") + 1;
                        nodeName2 = f2.Substring(n, f2.Length - n);
                        if (!f2.Contains(".htm") && !f2.Contains(".txt"))//判断文件类型
                        {
                            continue;
                        }
                        if ((newArticles = FileToXX(f2)) != null)//读取
                        {
                            if (newArticles.Equals(""))
                            {
                                dalite.Add(nodeName2, newArticles);
                                continue;
                            }
                            if (dalite.Values.Where(d => (d.Equals(newArticles))).Count() == 0)
                            {
                                dalite.Add(nodeName2, newArticles);
                            }
                        }
                    }
                    if (Templates == null)
                    {
                        Templates = new Dictionary<string, Dictionary<string, SendTemplate>>();
                    }
                    Templates.Add(nodeName, dalite);
                }
                return Templates;
            }
            else
            {
                Directory.CreateDirectory(path + "\\new");//文件夹不存在创建文件夹
                return TemplateInit();
            }
        }
        /// <summary>
        /// 读取路径,返回集合对象
        /// </summary>
        /// <param name="fileStr">路径</param>
        /// <returns>一个文件里的集合类</returns>
        public static SendTemplate FileToXX(string fileStr)
        {
            SendTemplate temp = new SendTemplate();
            temp.TemplateList = new List<SendTemp>();
            temp.FileName = fileStr;
            FileStream fs = new FileStream(fileStr, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string txtValue = "";
            string formId = "";
            string sendType = "";
            string s = "";
            string[] str = null;
            List<string> list = new List<string>();

            try
            {
                while (!sr.EndOfStream)
                {
                    s = sr.ReadLine();
                    str = s.Split('\t');
                    txtValue = str[2];
                    formId = str[0];
                    sendType = str[1];
                    temp.TemplateList.Add(new SendTemp { FormId = int.Parse(formId), SendTypeId = int.Parse(sendType), TextValue = txtValue });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                sr.Close();
                fs.Close();
            }
            return temp;
        }
        /// <summary>
        /// 创建指定后缀,并且文件名不重复
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="connent">内容</param>
        /// <returns>新实体</returns>
        public static string CreateEntity(string directoryName, string str)
        {
            string s = ".html";//生成文件名
            string z = ".html";//后缀
            string path = TManager.path + "\\" + directoryName + "\\" + str.Substring(0, 1);//组装路径
            for (int i = 0; true; i++)//动态生成文件名,防止重复
            {
                if (!File.Exists(path + s))
                {
                    break;
                }
                s = "_" + i + z;
            }
            if (TManager.UpdateContent(path, str))
            {
                return "成功";
            }
            return null;
        }
        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="connent">内容</param>
        /// <returns>是否成功</returns>
        public static bool UpdateContent(string path, string content)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(content);
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            return true;
        }
    }

 

posted @ 2010-12-20 10:26  紫荆飞翔  阅读(243)  评论(0编辑  收藏  举报