【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/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现