zoukankan      html  css  js  c++  java
  • 文件解压缩,删除文件,创建文件,读取xml文件为json字符串,得到相对路径下的图片名称

    1.读取xml文件为json字符串

     /// <summary>
            /// 读取xml文件为json字符串
            /// </summary>
            /// <param name="path">xml的路径</param>
            /// <returns>返回的json字符串</returns>
            public static String Readxml(String path)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                string jsonText = JsonConvert.SerializeXmlNode(doc).Replace("@", "");
                var model = jsonText.Remove(0, 45);
                var module = model.Substring(0, model.Length - 1);
                return module;
            }

    2.创建新的文件夹 默认参数为路径下

     /// <summary>
            /// 为模块新类型创建文件夹
            /// </summary>
            /// <param name="FileName">文件</param>
            /// <param name="path">默认路径</param>
            /// <returns></returns>
            public static Boolean CreateFiles(String FileName, String path = @"....Contentmodules")
            {
                Boolean result = false;
                String newPath = Path.Combine(HttpContext.Current.Server.MapPath(path), FileName);//拼接路径
                if (!Directory.Exists(newPath))//判断目录下是否存在同名文件夹
                {
                    try
                    {
                        Directory.CreateDirectory(newPath);//创建文件夹
                        result = true;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                return result;
            }

    3.解zip压缩文件

     /// <summary>
            /// 解zip压缩方法
            /// </summary>
            /// <param name="path">压缩文件的相对路径</param>
            /// <returns></returns>
            public static Boolean Decompression(String path)
            {
                Boolean isTrue = false;
                try
                {   //必须使用using不然解压完线程依旧占用zip文件,无法删除。O(∩_∩)O哈哈~
                    using (ZipFile zip = ZipFile.Read(path))
                    {
                        foreach (ZipEntry z in zip)
                        {
                            z.Extract(path.Substring(0, path.Length - 4), ExtractExistingFileAction.OverwriteSilently);
                        }
                        isTrue = true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return isTrue;
            }

    4.删除指定路径(绝对)下的文件、文件夹

     /// <summary>
            /// 删除指定路径下的文件
            /// </summary>
            /// <param name="path">删除文件的路径【绝对】</param>
            /// <returns></returns>
            public static Boolean DeleteFile(String path)
            {
                Boolean isTrue = false;
    
                FileAttributes attr = File.GetAttributes(path);//判断是文件还是文件夹
                if (attr == FileAttributes.Directory)
                {
                    if (!File.Exists(path))///判断路径是否存在
                    {   //删除文件夹
                        Directory.Delete(path, true);
                        isTrue = true;
                    }
                }
                else
                {
                    if (File.Exists(path))
                    {   //删除文件
                        File.Delete(path);
                        isTrue = true;
                    }
                }
               
                return isTrue;
            }

    5.根据文件夹路径得到里面的图片名称与格式

     /// <summary>
            /// 根据文件夹的路径得到文件夹下面的图片名
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static String GetImgName(String path)
            {
                //Todo 用于解压缩后得到其中的图片格式和图片名
                string name = "";
                DirectoryInfo di = new DirectoryInfo(path);//找到图片所在的路径
                FileInfo[] fiArray = di.GetFiles();      //读取该路径下所有文件           
                foreach (FileInfo fi in fiArray)
                {
                    string fileType = fi.Name.Substring(fi.Name.LastIndexOf(".") + 1);//取后缀名
                    if (fileType == "png" || fileType == "jpg" || fileType == "gif" || fileType == "jpeg" || fileType == "bmp")//筛选图片格式
                    {
                        name =  fi.Name;
                    }
                }
                return name;
            }
  • 相关阅读:
    spring mvc配置完后实现下载功能
    表单中Readonly和Disabled的区别(转载)
    EL表达式中fn函数 (转载)
    Spring mvc中@RequestMapping 6个基本用法小结(转载)
    web开发,关于jsp的常见问题,重复提交,防止后退。
    JQuery页面加载
    解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)
    数据库设计原则(转载)
    dhtmlxTree介绍(转载)
    主键索引
  • 原文地址:https://www.cnblogs.com/shichina/p/11398334.html
Copyright © 2011-2022 走看看