zoukankan      html  css  js  c++  java
  • 【ASP.NET】复制单个文件同时到多个目录

    有时候,当我们更新了一个dll文件后,需要将该dll文件复制到到不同的文件夹中,手动操作会很麻烦,因此可以考虑利用程序实现。

    利用powershell批量复制


    示例代码如下:

    $source="C:\WebMVC\web.config"
    $destination = @("D:\a\","D:\b\","D:\c\")
    foreach ($dir in $destination)
    {
         Copy-Item -Path $source -Destination ($dir+$source)
    }

    上述的代码可以把C盘内的一个web.config文件复制到D盘的三个不同的位置,但是输入$source和$destination非常麻烦,不太适合批量操作。

    对Powershell代码进行改进,通过GUI的方式选择文件,并且允许用户输入多个目标位置,代码如下:

    $myDialog = New-Object System.Windows.Forms.OpenFileDialog
    $myDialog.Title = “Please select a file”
    $myDialog.InitialDirectory = “C:Windows”
    $myDialog.Filter = “All Files (*.*)|*.*”
     
    $result = $myDialog.ShowDialog()
     
    If($result -eq “OK”) {
     
        $source = $myDialog.FileName
     
        $textBox = New-Object System.Windows.Forms.TextBox
        $textBox.Location = New-Object System.Drawing.Size(10,40)
        $textBox.Size = New-Object System.Drawing.Size(575,200)
        $textBox.AcceptsReturn = $true
        $textBox.AcceptsTab = $false
        $textBox.Multiline = $true
        $textBox.ScrollBars = 'Both'
        $textBox.Text = $DefaultText
     
        # Create the OK button.
        $okButton = New-Object System.Windows.Forms.Button
        $okButton.Location = New-Object System.Drawing.Size(415,250)
        $okButton.Size = New-Object System.Drawing.Size(75,25)
        $okButton.Text = "OK"
        $okButton.Add_Click({
             
            $textBox.Text;
            $paths=$textBox.Lines;
            foreach($path in $paths)
            {
                Copy-Item -Path $source -Destination ($path)
            }
            $form.Close()
            Write-Host("success!")
        })
     
        $form = New-Object System.Windows.Forms.Form
        $form.Text = $WindowTitle
        $form.Size = New-Object System.Drawing.Size(610,320)
        $form.FormBorderStyle = 'FixedSingle'
        $form.StartPosition = "CenterScreen"
        $form.AutoSizeMode = 'GrowAndShrink'
        $form.Topmost = $True
        $form.AcceptButton = $okButton
        $form.CancelButton = $cancelButton
        $form.ShowInTaskbar = $true    
     
        $form.Controls.Add($textBox)
        $form.Controls.Add($okButton)
        $form.Add_Shown({$form.Activate()})
        $form.ShowDialog()
    }
    else {
        Write-Host “Cancelled by user”
    }
     

    使用时,Powershell会自动打开文件选择窗口,待用户选择好文件后,会弹出一个文本框,用户可以输入目标路径,每行代表一个路径。输入完成点击OK后,源文件便会被同时复制到多个目标路径。

    实际上,目标路径在输入的时候还是比较麻烦,逐条输入甚至还不如打开窗口,一个一个复制来的便捷。

    为了更快的输入目标路径,推荐使用everything,搜索需要替换的目标文件名,通过批量复制文件路径的方式,可以快速获取到目标路径。

    利用Windows Forms批量复制


    Powershell编写GUI程序比较复杂,同时难以调试,采用Windows Forms的方式编程更加快捷。示例代码如下,可以方便进行批量复制操作。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
     
    namespace CopyFileToMultiPath
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void copyBtn_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] desPaths = desPathsTextBox.Lines;
                    foreach (var path in desPaths)
                    {
                        var desPath = path;
                        if(File.GetAttributes(path).HasFlag(FileAttributes.Directory))
                        {
                            if(path.LastOrDefault()=='\')
                            {
                                desPath = path + Path.GetFileName(filePathTextBox.Text);
                            }
                            else
                            {
                                desPath = path + "\" + Path.GetFileName(filePathTextBox.Text);
                            }
                        }
                        File.Copy(filePathTextBox.Text, desPath, true);
                    }
                    MessageBox.Show("Copy success!");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Copy failed! "+ex.Message);
                }
            }
     
            private void selectBtn_Click(object sender, EventArgs e)
            {
                OpenFileDialog choofdlog = new OpenFileDialog();
                choofdlog.Filter = "All Files (*.*)|*.*";
                choofdlog.FilterIndex = 1;
                choofdlog.Multiselect = false;
     
                if (choofdlog.ShowDialog() == DialogResult.OK)
                {
                    string sFileName = choofdlog.InitialDirectory + choofdlog.FileName;
                    filePathTextBox.Text = sFileName;
                }
            }
        }
    }
    最新博客地址:http://blog.turenlong.com/

  • 相关阅读:
    计数和查找
    遍历
    top小火箭
    leetcode 字符串中的第一个唯一字符
    leetcode 颠倒整数
    leetcode 反转字符串
    leetcode 有效的数独
    leetcode 两数之和
    leetcode 移动零
    leetcode 加一
  • 原文地址:https://www.cnblogs.com/reachteam/p/5774507.html
Copyright © 2011-2022 走看看