C# WinForm拖拽文件+获取exe程序所在目录上上上级目录+复制单个文件并保存+复制某文件夹内文件到另一文件夹

【基于C# winform 界面拖拽pdf文件+调用Qpdf的小程序解密后+另存到新文件夹】代码

C# WinForm
C#拖拽文件
C#获取exe程序所在目录上上上级目录
C#复制单个文件并保存
C#复制某文件夹内文件到另一文件夹

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WinFormsApp1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        void Init() {
            this.AllowDrop = true; //设置为允许拖拽
        }


        private void ListView1_DragEnter(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                e.Effect = DragDropEffects.Link;
            }
            else {
                e.Effect = DragDropEffects.None;
            }

        }

        string str01;

        private void ListView1_DragDrop(object sender, DragEventArgs e) {
            IDataObject ido = e.Data
            if (ido.GetDataPresent(DataFormats.FileDrop))       //如果拖动的数据是文件类型
            {
                string[] filepaths = (string[])ido.GetData(DataFormats.FileDrop);  //获取文件的路径
                DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
                
                string exePath = info.Parent.Parent.Parent.FullName;//上上上级目录
                string paths01 = Path.Combine(exePath, "output");
                
                System.IO.Directory.CreateDirectory(paths01);//在上上上级目录下创建output文件夹,如果存在也不会重复创建01
                DirectoryInfo dir01 = new DirectoryInfo(paths01);
                dir01.Delete(true);//删除output文件夹
                System.IO.Directory.CreateDirectory(paths01);//创建onput文件夹,如果存在也不会重复创建01
                
                string paths02 = Path.Combine(exePath, "input");
                System.IO.Directory.CreateDirectory(paths02);//创建input文件夹,如果存在也不会重复创建02
                DirectoryInfo dir02 = new DirectoryInfo(paths02);
                dir02.Delete(true);//删除input文件夹
                System.IO.Directory.CreateDirectory(paths02);//创建input文件夹,如果存在也不会重复创建02

                //执行我们想要执行的业务逻辑,读取复制等等。
                foreach (var item in filepaths) {
                    string extension = System.IO.Path.GetExtension(item);//
                    string A = ".pdf";
                    if (String.Compare(extension, A) == 0) {
                        FileInfo f = new FileInfo(item);
                        string fname = Path.GetFileName(item);//带扩展名的文件名

                        string paths03 = Path.Combine(paths02, fname);//路径与文件名合并为新路径paths03

                        f.CopyTo(paths03);//单个文件复制到paths03 ;因为paths03是包含“带扩展名的文件名”的存储路径,故复制的文件不会重命名,文件名与原文件一致                       
                    }
                    else {
                        MessageBox.Show("error!");
                        str01 = "0";
                    }
                }



                Process proc = null;
                try {
                    string targetDir = string.Format(exePath);//需调用的程序 unlockpdf.bat 在目录exePath中
                    proc = new Process();
                    proc.StartInfo.WorkingDirectory = targetDir;
                    proc.StartInfo.FileName = "unlockpdf.bat";
                    proc.StartInfo.Arguments = string.Format("10");//this is argument//调用程序时的参数
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
                    proc.Start();
                    proc.WaitForExit();
                }
                catch (Exception ex) {
                    Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
                }




                // MessageBox.Show("解除密码完成!");
                if (String.Compare(str01, "0") != 0) {
                    FolderBrowserDialog dilog = new FolderBrowserDialog();
                    dilog.Description = "请选择保存解密结果文件夹";
                    if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes) {
                        string saveDirPath = dilog.SelectedPath;
                        try {
                            if (!Directory.Exists(saveDirPath)) {
                                Directory.CreateDirectory(saveDirPath);
                            }
                            string[] files = Directory.GetFiles(paths01);//获取output文件夹里的所有文件
                            foreach (string file in files) {
                                string pFilePath = saveDirPath + "\\" + Path.GetFileName(file);
                                if (File.Exists(pFilePath))
                                    continue;
                                File.Copy(file, pFilePath, true);//将文件保存到选择的文件夹
                            }
                        }
                        catch (Exception ex) {
                            MessageBox.Show(ex.Message);
                        }


                    }
                }



            }
            else {
                MessageBox.Show("error!");
            }

        }
    }
}

本人C#小白,摸索查询了一整天搞了这个东东。
用途与背景:PDF文档密码破解(https://zhuanlan.zhihu.com/p/74141259)
压缩文件:PDFunlocker界面+【Qpdf】version-8.4.2-64.zip
效果图如下:
在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值