zoukankan      html  css  js  c++  java
  • .net 创建本地文件夹,在网站中创建文件夹

    protected void Button1_Click(object sender, EventArgs e)
        {
            string path = "";
            string physicsPath = Server.MapPath(Request.ApplicationPath); //将当前虚拟根路径转为实际物理路径
            string toFindDirectoryName = "ss"//要查找的文件夹名
            FindDirectory(physicsPath + "\\", toFindDirectoryName, out path);//用递归的方式去查找文件夹
            if (!string.IsNullOrEmpty(path)) //如果存在,返回该文件夹所在的物理路径
            {
                //将该物理路径转为虚拟路径
                Response.Write(GetVirtualPath(path, Request.ApplicationPath));
            }
            else
            {
                //没有找到路径,创建新文件夹
                Directory.CreateDirectory(physicsPath + "\\" + toFindDirectoryName);
            }
        }

        /// <summary>
        
    /// 将物理路径转为虚拟路径
        
    /// </summary>
        
    /// <param name="physicsPath">物理路径</param>
        
    /// <param name="virtualRootPath">虚拟根路径</param>
        
    /// <returns></returns>
        private string GetVirtualPath(string physicsPath, string virtualRootPath)
        {
            int index = physicsPath.IndexOf(virtualRootPath.Substring(1));
            return "/" + physicsPath.Substring(index).Replace("\\""/");
        }

        /// <summary>
        
    /// 在指定目录下递归查找子文件夹
        
    /// </summary>
        
    /// <param name="bootPath">根文件夹路径</param>
        
    /// <param name="directoryName">要查找的文件夹名</param>
        private void FindDirectory(string bootPath, string directoryName, out string filePath)
        {
            //在指定目录下递归查找子文件夹
            DirectoryInfo dir = new DirectoryInfo(bootPath);
            filePath = "";
            try
            {
                foreach (DirectoryInfo d in dir.GetDirectories()) //查找子文件夹
                {
                    if (d.Name == directoryName) //找到,返回文件夹路径
                    {
                        filePath = d.FullName;
                        break;
                    }
                    FindDirectory(bootPath + d.Name + "\\", directoryName, out filePath); //否则继续查找
                }
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }

        }
  • 相关阅读:
    艾伟:[WCF中的Binding模型]之六(完结篇):从绑定元素认识系统预定义绑定 狼人:
    艾伟:.NET框架4.0中都有些什么? 狼人:
    艾伟:WM有约(三):下一次是什么时候? 狼人:
    艾伟:为什么微软要推 ADO.NET Data Services Framework 狼人:
    艾伟:WM有约(二):配置信息 狼人:
    艾伟:F4何去何从 大视野观察Framework 4.0 狼人:
    艾伟:[WCF的Binding模型]之三:信道监听器(Channel Listener) 狼人:
    艾伟:.NET : 如何保护内存中的敏感数据? 狼人:
    艾伟:Silverlight 2.0 之旋转木马 狼人:
    艾伟:.NET和J2EE该相互学习什么 狼人:
  • 原文地址:https://www.cnblogs.com/hishanghai/p/2558837.html
Copyright © 2011-2022 走看看