生成文件和删除文件操作!

 public class CSVHelper

    {

        /// <summary>

        /// 专利导出

        /// </summary>

        /// <typeparam name="TEntity"></typeparam>

        /// <param name="directory"></param>

        /// <param name="objs"></param>

        /// <returns></returns>

        public static string CreateCSV(string directory, List<PatentInfo> objs, Dictionary<string, string> patentFields)

        {

            string outputString = "";

            patentFields.Remove("SYSID");

            foreach (string field in patentFields.Keys)

            {

                if (!string.IsNullOrEmpty(field))

                {

                    string temp = field;

                    temp = temp.Replace("/"", "/"/"");

                    temp = "/"" + temp + "/",";

                    outputString += temp;

                }

            }

            outputString += "/r/n";

 

            for (int i = 0; i < objs.Count; i++)

            {

                for (int j = 0; j < patentFields.Count(); j++)

                {

                    string temp = null;

                    try

                    {

                        temp = objs[i].Fields[patentFields.Values.ToList()[j]];

                    }

                    catch

                    { temp = null; }

                    temp = temp == null ? "" : temp.Replace("<span class=/"highlightspan/">", "").Replace("</span>", "");

                    temp = temp.Replace("/"", "/"/"");

                    temp = "/"" + temp + "/",";

                    outputString += temp;

                }

                outputString += "/r/n";

            }

            outputString += "/r/n";

 

            string path = directory;

 

            if (!path.EndsWith("//")&&!path.EndsWith("/"))

            {

                path += "//";

            }

 

            string fileName = Math.Abs(DateTime.Now.GetHashCode()).ToString() + ".csv";

            try

            {

                using (StreamWriter sw = new StreamWriter(path + fileName, false, Encoding.GetEncoding("gb2312")))

                {

                    sw.Write(outputString);

                }

            }

            catch

            {

                string[] pathsStr = GetFileNames(path, fileName.Substring(0, fileName.Length - 4)+"*");

                if (pathsStr != null && pathsStr.Length > 0)

                {

                    foreach (string pathX in pathsStr)

                    {

                        if (File.Exists(pathX))

                        {

                            File.Delete(pathX);

                        }

                    }

                }

                return null;

            }

            return fileName;

        }

 

        //返回指定目录下的文件名称

        /// <summary>

        /// 

        /// </summary>

        /// <param name="directoryPath">指定目录</param>

        /// <param name="searchPattern">文件名称</param>

        /// <returns></returns>

        public static string[] GetFileNames(string directoryPath, string searchPattern)

        {

            //如果目录不存在,则抛出异常

            if (!Directory.Exists(directoryPath))

            {

                throw new FileNotFoundException("当前路径不存在!");

            }

            try

            {

                return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);

            }

            catch

            {

                return null;

            }

        }

 

        //删除文件

        /// <summary>

        /// 

        /// </summary>

        /// <param name="directoryPath">文件路径</param>

        /// <returns></returns>

        public static bool DeleteFile(string directoryPath)

        {

            try

            {

                //如果目录不存在,则抛出异常

                if (!File.Exists(directoryPath))

                {

                    throw new FileNotFoundException("当前路径不存在!");

                }

                File.Delete(directoryPath);

                return true;

            }

            catch

            {

                return false;

            }

        }

 

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值