0

我有一个程序可以创建指向我搜索的文件的超链接列表。我可以单击生成的超链接并打开 PDF。我还想让它在我的 C: 上制作 pdf 的副本:也没有对话框。文件来源来自网络。FileSearchButton_Click 将在目录中搜索内容与我的搜索字符串匹配的文件。

aLinkLabel_LinkClicked 是我觉得应该完成复制操作的地方

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

namespace FindFilesBasedOnText
{
    public partial class FileSearcherBasedOnSpecificText : Form
    {
        SmartTextBox DirectoryTextBox = new SmartTextBox();
        SmartTextBox SearchTextBox = new SmartTextBox();
        SmartButton SearchButton = new SmartButton();
        SmartButton FileSearchButton = new SmartButton();
        public FileSearcherBasedOnSpecificText()
        {
            InitializeComponent();
            DirectoryTextBox.Location = new Point(70, 12);
            DirectoryTextBox.ForeColor = Color.Black;
            DirectoryTextBox.ForeColor = Color.Black;
            this.Controls.Add(DirectoryTextBox);

            SearchTextBox.Location = new Point(70, 43);
            //SearchTextBox.Size = new Size(478, 20);
            this.Controls.Add(SearchTextBox);

            SearchButton.Location = new Point(526, 12);
            SearchButton.Size = new Size(160, 23);
            SearchButton.Text = "Search Directory";
            SearchButton.TabStop = false;
            SearchButton.Click += SearchButton_Click;
            this.Controls.Add(SearchButton);

            FileSearchButton.Location = new Point(526, 43);
            FileSearchButton.Size = new Size(160, 23);
            FileSearchButton.Text = "Search file based-on text";
            FileSearchButton.TabStop = false;
            FileSearchButton.Click += FileSearchButton_Click;
            this.Controls.Add(FileSearchButton);

            listBox1.AllowDrop = true;
            listBox1.DragDrop += listBox1_DragDrop;
            listBox1.DragEnter += listBox1_DragEnter;
            listBox1.Focus();


        }


        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in files)
                listBox1.Items.AddRange(File.ReadAllLines(file));        
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            DialogResult dialogReasult = folderBrowserDialog.ShowDialog();

            if(!string.IsNullOrWhiteSpace(folderBrowserDialog.SelectedPath))
            {
                DirectoryTextBox.Text = folderBrowserDialog.SelectedPath;
            }
        }

        public void FileSearchButton_Click(object sender, EventArgs e)
        {
            FilesPanel.Controls.Clear();
            int i = 0; 
            int y = 5;
            string filesName = string.Empty;
            string rootfolder = DirectoryTextBox.Text.Trim();
            string[] files = Directory.GetFiles(rootfolder, "*.*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                try
                {
                    string contents = File.ReadAllText(file);
                    if(contents.Contains(SearchTextBox.Text.Trim()))
                    {
                        i += 1;
                        LinkLabel aLinkLabel = new LinkLabel();
                        aLinkLabel.Text = file;
                        aLinkLabel.Location = new Point(5, y);
                        aLinkLabel.AutoSize = true;
                        aLinkLabel.BorderStyle = BorderStyle.None;
                        aLinkLabel.LinkBehavior = LinkBehavior.NeverUnderline;
                        aLinkLabel.ActiveLinkColor = Color.White;
                        aLinkLabel.LinkColor = Color.White;
                        aLinkLabel.BackColor = Color.Transparent;
                        aLinkLabel.VisitedLinkColor = Color.Red;
                        aLinkLabel.Links.Add(0, file.ToString().Length, file);
                        aLinkLabel.LinkClicked += aLinkLabel_LinkClicked;
                        FilesPanel.Controls.Add(aLinkLabel);
                        y += aLinkLabel.Height + 5;
                    }
                    else
                    {    // This shows DONE after each Search
                        LinkLabel aLinkLabel = new LinkLabel();  
                        aLinkLabel.Font = new Font("", 12);
                        aLinkLabel.ActiveLinkColor = Color.White;
                        aLinkLabel.LinkColor = Color.White;
                        aLinkLabel.BackColor = Color.Transparent;
                        aLinkLabel.Text = "DONE";
                        FilesPanel.Controls.Add(aLinkLabel);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }



        void aLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel lnk = new LinkLabel();
            lnk = (LinkLabel)sender;
            lnk.Links[lnk.Links.IndexOf(e.Link)].Visited = true;
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());

            //Create a directory if not exist for the file to be copied into
             if (!System.IO.Directory.Exists(@"C:\Temp\RoHS_Docs"))
            {
                System.IO.Directory.CreateDirectory(@"C:\Temp\RoHS_Docs");
            }

            //Perform the file copy action on click
            string CopyDestinationPath = @"C:\Temp\RoHS_Docs\";
            System.IO.File.Copy(WHAT????, CopyDestinationPath, true);
        }

        private void FileSearcherBasedOnSpecificText_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FilesPanel.Controls.Clear();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchTextBox.Text = listBox1.SelectedItem.ToString();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }
    }
}
4

0 回答 0