• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

利用ASP.NET,把多个图片文件夹归类,归类到一起

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

代码如下:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebPutImagesTogether._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>把多个图片文件夹归类,归类到一起</title>
    <style type="text/css">
        .redBorder { border:solid 1px red; }
        .greenBorder { border:solid 1px green; }
        .marginLeft7em { margin-left: 7em; }
    </style>
</head>
<body>
    <form >
    <div>
        <h1>把多个图片文件夹归类,归类到一起</h1>
    </div>
    <div>
        <a href="RenameFiles.aspx" target="_blank">把文件夹“Root”下的所有文件夹“Parent”下的文件重命名为“ParentXXX”</a>
    </div>
    <div>
        <asp:Label ID="lblMessage" runat="server" />
    </div>
    <asp:PlaceHolder ID="phDealResult" runat="server" Visible="false">
        <div class="greenBorder">
            成功处理的文件夹目录为:<br /><br /><asp:Label ID="lblSuccess" runat="server" />
        </div>
        <div class="redBorder">
            处理失败的文件夹目录为:<br /><br /><asp:Label ID="lblError" runat="server" />
        </div>
    </asp:PlaceHolder>
    <div>
        请输入要处理的图片文件夹目录:<asp:TextBox ID="txtSourceFolder" runat="server" Width="70%" /><br /><br />
        请输入<span class="marginLeft7em"></span>输出目录:<asp:TextBox ID="txtDestinationFolder" runat="server" Width="70%" /><br /><br />
        <asp:Button ID="btnTest" runat="server" Text="开始测试" OnClick="btnTest_Click" />
            
        <asp:Button ID="btnOk" runat="server" Text="开始处理" OnClick="btnOk_Click" />
    </div>
    </form>
</body>
</html>

  

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace WebPutImagesTogether
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="rightFlag">是否是正确的消息,为 Null,表示不设置颜色。如果是,则消息的颜色为绿色,否则为红色</param>
        protected void ShowMessage(string message, bool? rightFlag)
        {
            this.lblMessage.Text = message;
            if (rightFlag.HasValue)
            {
                if (rightFlag.Value)
                {
                    this.lblMessage.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    this.lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                this.lblMessage.ForeColor = System.Drawing.Color.Black;
            }
            
        }

        /// <summary>
        /// 开始测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnTest_Click(object sender, EventArgs e)
        {
            DealCommon(true);
        }

        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="isTest">是否是测试</param>
        protected void DealCommon(bool isTest)
        {
            string sourceFolderPath = this.txtSourceFolder.Text;
            string destinationFolderPath = this.txtDestinationFolder.Text;
            if (!Directory.Exists(sourceFolderPath))
            {
                ShowMessage("要处理的图片文件夹目录不存在", false);
                return;
            }
            if (!Directory.Exists(destinationFolderPath))
            {
                ShowMessage("输出目录不存在", false);
                return;
            }
            if (destinationFolderPath[destinationFolderPath.Length - 1] != '\\')
            {
                destinationFolderPath += '\\';
            }
            DirectoryInfo destinationFolder = new DirectoryInfo(destinationFolderPath);
            string[] sourceAllChildFolders = Directory.GetDirectories(sourceFolderPath);
            string folderSuffixName = "_files";
            string[] sourceRightChildFolders = Directory.GetDirectories(sourceFolderPath, "*" + folderSuffixName, SearchOption.TopDirectoryOnly);
            StringBuilder messageAppend = new StringBuilder();
            StringBuilder errorMessageAppend = new StringBuilder();

            DirectoryInfo currentDir;
            string trueFolderName;
            foreach (string item in sourceRightChildFolders)
            {
                currentDir = new DirectoryInfo(item);
                trueFolderName = currentDir.Name.Substring(0, currentDir.Name.Length - folderSuffixName.Length);
                string endWithNumRegex = @"[1-9]\d*$"; //以数字结尾
                if (Regex.IsMatch(trueFolderName, endWithNumRegex))
                {
                    trueFolderName = Regex.Replace(trueFolderName, endWithNumRegex, string.Empty);
                }
                if (isTest)
                {
                    messageAppend.Append(trueFolderName + "<br/>");
                }
                else
                {
                    FileInfo[] imgFiles = currentDir.GetFiles("*.jpg");
                    string newImgFilePath;
                    foreach (FileInfo imgFileItem in imgFiles)
                    {
                        newImgFilePath = destinationFolder.FullName + trueFolderName + imgFileItem.Name;
                        if (!File.Exists(newImgFilePath))
                        {
                            imgFileItem.CopyTo(newImgFilePath);
                        }
                        else
                        {
                            errorMessageAppend.Append("准备复制时,可目标文件已经存在!文件路径:" + newImgFilePath + "<br/>");
                        }
                    }
                }
            }
            foreach (string item in sourceAllChildFolders)
            {
                if (!sourceRightChildFolders.Contains(item))
                {
                    errorMessageAppend.Append("无法处理这样的文件夹:" + item + "<br/>");
                }
            }
            this.phDealResult.Visible = true;
            this.lblSuccess.Text = messageAppend.ToString();
            this.lblError.Text = errorMessageAppend.ToString();
        }

        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOk_Click(object sender, EventArgs e)
        {
            DealCommon(false);
        }
    }
}

  

谢谢浏览!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net 子窗体刷新父窗体发布时间:2022-07-10
下一篇:
asp.net图片上传实例发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap