0

I have a program that creates a list of hyperlinks to the files i search for. I can click the resulting hyperlink and the PDF opens. I would like to also have it make a copy of the pdf on my C: also without dialog. The source of the files is from the network. FileSearchButton_Click will search a directory for files with the content that match my search string.

aLinkLabel_LinkClicked is where I feel the copy action should be done

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)
        {

        }
    }
}
Ultrax
  • 1
  • 3
  • Where does the source file actually exist on disk? That is what you need to put there. If not you need to capture the stream from your process start, collect it in a buffer, and then use System.IO.File.WriteAllBytes or System.IO.File.WriteAllText. – Mr. B Mar 13 '20 at 17:17
  • Hi. Isn't it `e.Link.LinkData.ToString()` ? What is the value of this? – Oguz Ozgul Mar 13 '20 at 17:33
  • The source is a network path. – Ultrax Mar 13 '20 at 17:41
  • Try the network path. If it doesn't work, edit your question accordingly. Show us your network path. – Robert Harvey Mar 13 '20 at 18:01
  • Oguz Ozgul -- e.Link.LinkData.ToString() --> I looked at this output and it does contain the full path with filename. I could use this to split it into path + filename in order to use File.WriteAllText. i will try – Ultrax Mar 13 '20 at 19:57

0 Answers0