How to use path of the selected files to apply the Method to these selected files

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello, everyone!!

A have a trouble...
Because I want to apply method searchMask to the selected items by pushing button8.

I dont understand, how to connect them, because I dont know how to get path of files in string rootFolder in searchMask method


Could you help me with my challenge?



C#:
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;
using System.Text.RegularExpressions;
using System.Collections;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
  
        private void button5_Click(object sender, EventArgs e) 

        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                string[] files = Directory.GetFiles(FBD.SelectedPath);
                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
            
            foreach (string file in files)
                {
                    listBox1.Items.Add(Path.GetFileName(file));
                }
                listBox1.Sorted = true;
                foreach (string dir in dirs)
                {
                    listBox1.Items.Add(Path.GetFileName((dir)));
                }
            }
        }
        private void button8_Click(object sender, EventArgs e) 
        {
            {
                ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(listBox1);
                selectedItems = listBox1.SelectedItems;
                if (listBox1.SelectedIndex != -1)
                {
                    for (int i = selectedItems.Count - 1; i >= 0; i--)
                        searchMask();
                }
            }
        }
        static void searchMask() 
        {
            string rootFolder = @"H:\Test1";
            string pattern = @"\b(PlNamOld = FZG[0-9]*)\b";
            using (StreamWriter sw = File.CreateText(@"C:\Users\Anton\Desktop\?#folder\target.txt"))

            foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))
                {
                    using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            string newstring = line.Substring(0, 8);
                            Match match = Regex.Match(line, pattern, RegexOptions.IgnoreCase);
                            if (match.Success)
                                sw.WriteLine(file + " " + newstring + " " + match.Value);
                            else
                                sw.Write("");
                        }
                    }
                }
        }
    }
}
 
Last edited:
You can get rid of most of the code in that button8_Click method. You don't create a new collection because you already have one. SelectedItems IS that collection. Just loop over that:
foreach (string filename in listBox1.SelectedItems)
{
    // Use filename here.
}

Logic seems to dictate that your searchMask method have a parameter for a file name and, when you call that method, you pass the file name from the ListBox:
foreach (string filename in listBox1.SelectedItems)
{
    searchMask(filename);
}
 
Well
Mission impossible... I dont understand your solution...

C#:
        private void button7_Click(object sender, EventArgs e) 
            ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(listBox1);
            selectedItems = listBox1.SelectedItems;


            if (listBox1.SelectedIndex != -1)
            
            {
                for (int i = selectedItems.Count - 1; i >= 0; i--)
                    listBox1.Items.Remove(selectedItems[i]);
            }
            else
                MessageBox.Show("?????????? ?? ??????? ???????");
        }
        private void button8_Click(object sender, EventArgs e) // ??????? ???????????? ?????????? ???????? ?????? ?? ???????? (????????????)
        {
            {
                ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(listBox1);
                selectedItems = listBox1.SelectedItems;
                if (listBox1.SelectedIndex != -1)
                {
                    for (int i = selectedItems.Count - 1; i >= 0; i--)
                        searchMask();
                }
              
            }
        }


//          foreach (string filename in listBox1.SelectedItems)
//{
//    // Use filename here.
//}


//          foreach (string filename in listBox1.SelectedItems)
//{
//          searchMask(filename);
//}


    static void searchMask() // ????? ?????? ?? ?????


        {
            //string rootFolder = @"C:\Users\Anton\Desktop\?#folder\?????";
            string pattern = @"\b(PlNamOld.*)\b";
            using (StreamWriter sw = File.CreateText(@"C:\Users\Anton\Desktop\?#folder\target.txt"))


                foreach (var selectedItems in listBox1.selectedItems)
                {
                    using (StreamReader sr = new StreamReader(selectedItems))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            string newstring = line.Substring(0, 8);
                            Match match = Regex.Match(line, pattern, RegexOptions.IgnoreCase);
                            if (match.Success)
                                sw.WriteLine(selectedItems + " " + newstring + " " + match.Value);
                            else
                                sw.Write("");      
          }
            MessageBox.Show("Okay");
        }
 
You haven't added a parameter to your searchMask method so of course it's not going to work. Declaring method is programming 101 so we should have to explain that. You've already got other methods in there with parameters too so you've got examples to work from. If you don't know the basics like declaring a method with a parameter then you should spend some time improving your fundamentals before trying to do anything more complex. If you don't then you're going to keep getting tripped up by simple things. There's a link to a tutorial in my signature below and I recommend that you follow it and work through the whole thing. By the end of it, if someone tells you to add a parameter to a method, you'll know what that means and be able to do it.
 
So I've shown you how to call the method with an argument. As I said, you have to declare it with a parameter:
static void searchMask(string fileName)

You then use that parameter in the method like you would any other variable.
 

Latest posts

Back
Top Bottom