//【Form1】如下:
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_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
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);//获取可执行文件exe的路径
string exePath = info.FullName;//exe程序所在文件夹X
string paths01 = Path.Combine(exePath, "bin","output");
string batPath = Path.Combine(exePath, "bin");//文件夹X里有个bin文件夹
System.IO.Directory.CreateDirectory(paths01);//在bin文件夹里创建output文件夹,如果存在也不会重复创建01
DirectoryInfo dir01 = new DirectoryInfo(paths01);
dir01.Delete(true);//删除output文件夹
System.IO.Directory.CreateDirectory(paths01);//创建onput文件夹,如果存在也不会重复创建01
string paths02 = Path.Combine(exePath,"bin", "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);//带扩展名的PDF文件名
string paths03 = Path.Combine(paths02, fname);//input路径与PDF文件名合并为新路径paths03
f.CopyTo(paths03);//单个文件复制到paths03 ;因为paths03是包含“带扩展名的PDF文件名”的存储路径,故复制的文件不会重命名,文件名与原文件一致
}
else
{
MessageBox.Show("error!");
str01 = "0";
}
}
Process proc = null;
try
{
string targetDir = string.Format(batPath);//需调用的程序 unlockpdf.bat 在目录batPath中
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 + "\\" + "Unlocked" + Path.GetFileName(file) ;
if (File.Exists(pFilePath))
continue;
File.Copy(file, pFilePath, true);//将文件保存到选择的文件夹
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
else
{
MessageBox.Show("error!");
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Form = new Form2();
Form.ShowDialog(); //弹出窗口2
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
注意1:
注意2:
C# Winform程序运行时,界面模糊怎么解决