0

How do I pass a file path taken from a dropped file to be the path in another function/method?

In a C# windows forms program, I have listbox I can drag and drop a file into, and it displays the file path in that list box:

 public Form1()
        {
            InitializeComponent();
            this.listBox1.DragDrop += new
           System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
            this.listBox1.DragEnter += new
                       System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
        }
        // drag and drop process
        private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            var files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files.Length == 1 && listBox1.Items.Count == 0)
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            int i;
            for (i = 0; i < s.Length; i++)
                listBox1.Items.Add(s[i]);
        }

In another part of the program, firing on a button push, I can unzip all files within a set directory, but I want that directory to be what I dropped in the listbox above, not something set permanently in the code.

        public static void MyMethod3()
        {
            string startPath = @"C:\testfolder\testprop\practicefolder\";
            string extractPath = @"C:\testfolder\testprop\practicefolder\unzippedstuff";
            Directory.GetFiles(startPath, "*.zip", SearchOption.AllDirectories).ToList()
                .ForEach(zipFilePath =>
                {
                    var extractPathForCurrentZip = Path.Combine(extractPath, Path.GetFileNameWithoutExtension(zipFilePath));
                    if (!Directory.Exists(extractPathForCurrentZip))
                    {
                        Directory.CreateDirectory(extractPathForCurrentZip);
                    }
                    ZipFile.ExtractToDirectory(zipFilePath, extractPathForCurrentZip);
                });
        }

I actually want to pass that same path to a few other functions/methods/processes at the same time, but this seemed like the cleanest example.

Sorry if it's a stupid/easy question, or if I'm doing a number of things horribly wrong. I've tried a ton of things I find that look like they could work, but without success.

4
  • Can you not just pass the file path as an argument to the method that needs it (e.g. MyMethod3(string filePath))?
    – devNull
    Aug 23, 2020 at 16:21
  • You'd add a PARAMETER to "MyMethod3": public static void MyMethod3(string startPath). On the button click, grab the currently selected item in the ListBox and pass it to "MyMethod3"...
    – Idle_Mind
    Aug 23, 2020 at 16:21
  • "I've tried a ton of things I find that look like they could work, but without success" - you should include what you've tried already in the question, along with why those attempts didn't work. That way you won't get answers that are the same as what you've already. tried
    – devNull
    Aug 23, 2020 at 16:28
  • Where does MyMethod3 live? Where is the "button push" that triggers MyMethod3? How do they relate to Form1? The reason you don't have good answers is that you haven't given all of the detailed required to answer this properly. Aug 30, 2020 at 4:35

2 Answers 2

-1

Make a field and use it as a variable so it can be available for the whole class

0
-1

I ended up following this answer: How to make a variable available to all classes in XNA/monogame?

"Just create a static class where you will store all your global variables and it will be accessible from all your classes."

    public static class MyGlobals
    {
        public static string finalPathForWork { get; set; }
    }

I'm sure it's not the best way to do it, but it worked for now.

3
  • 1
    Please don't do this. Static classes with static properties will end up causing your app to become unmanagable. Aug 30, 2020 at 3:22
  • Can you be more specific about "unmanageable" or link to something that explains it? It's a very small/simple app, about 150 lines. Or suggest a better way to do it? I tried using what was in the comments above but nothing else worked. Aug 30, 2020 at 3:57
  • 1
    Static variables mean that any instance of any class can access the values and because of that your code creates many such links that inevitably become like tangled webs. If you manage the flow of data through instance (not static) variables then you can better reason with your code and that makes it manageable. Aug 30, 2020 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.