Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. [Resolved] Searching for a file in multiple directories?

[Resolved] Searching for a file in multiple directories?

Scheduled Pinned Locked Moved C#
questionalgorithmshelptutorial
14 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO OriginalGriff

    Goaty65109 wrote:

    that just gives me the paths to the directories that contain a file called settings.txt

    Not quite - if you add a button to your form and run that code in the button click, you will find it prints the full path to all the files called "Settings.txt". So all you have to do is feed the string into your IniFile constructor and access the file:

    IniFile ini = new IniFile(file);

    You can then add each setting value to your ComboBox Items collection. You don't want to set the Text property each time, because that will only show the last value you set into it - it is just a string property after all! :laugh:

    If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

    G Offline
    G Offline
    Goaty65109
    wrote on last edited by
    #5

    So let me make sure I understand this correctly, and please forgive me as I am completely novice and am running through as many training videos as I can all the while trying to dream big with my own personal projects. After I generate that information, I just need to plug this into a button (or form_load as intended): IniFile ini = new IniFile(file); comboBox = ini.IniReadValue("Info", "Name"); and it will simply fill my combo box with my desired settings? I have a feeling I am wrong as this sounds too simple, or maybe I just hit the jackpot on my biggest hurdle. Thank you so much for working with me. :)

    OriginalGriffO 1 Reply Last reply
    0
    • G Goaty65109

      So let me make sure I understand this correctly, and please forgive me as I am completely novice and am running through as many training videos as I can all the while trying to dream big with my own personal projects. After I generate that information, I just need to plug this into a button (or form_load as intended): IniFile ini = new IniFile(file); comboBox = ini.IniReadValue("Info", "Name"); and it will simply fill my combo box with my desired settings? I have a feeling I am wrong as this sounds too simple, or maybe I just hit the jackpot on my biggest hurdle. Thank you so much for working with me. :)

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #6

      Not quite that simple - you need to use both bits of code, and you can't assign a string to a ComboBox variable! :laugh: Try something like this:

      string[] SettingsFiles = Directory.GetFiles(@"D:\Temp\", "Settings.txt", SearchOption.AllDirectories);
      comboBox.Items.Clear();
      foreach (string file in SettingsFiles)
      {
      IniFile ini = new IniFile(file);
      comboBox.Items.Add(ini.IniReadValue("Info", "Name"));
      }

      BTW: you probably don't need the "Ini" part on the method name "IniReadValue" - it is implied by the fact that it's an IniFile method.

      If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      G 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Not quite that simple - you need to use both bits of code, and you can't assign a string to a ComboBox variable! :laugh: Try something like this:

        string[] SettingsFiles = Directory.GetFiles(@"D:\Temp\", "Settings.txt", SearchOption.AllDirectories);
        comboBox.Items.Clear();
        foreach (string file in SettingsFiles)
        {
        IniFile ini = new IniFile(file);
        comboBox.Items.Add(ini.IniReadValue("Info", "Name"));
        }

        BTW: you probably don't need the "Ini" part on the method name "IniReadValue" - it is implied by the fact that it's an IniFile method.

        If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

        G Offline
        G Offline
        Goaty65109
        wrote on last edited by
        #7

        You are absolutely amazing, thank you so much. I am still rebuilding, but I will get back with you with my results. Is there any way to give you props on this site? I would like to contribute to whatever the reputation tracker is. Thank you so much!

        OriginalGriffO 1 Reply Last reply
        0
        • G Goaty65109

          You are absolutely amazing, thank you so much. I am still rebuilding, but I will get back with you with my results. Is there any way to give you props on this site? I would like to contribute to whatever the reputation tracker is. Thank you so much!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #8

          You're welcome! You can vote on (almost) any message - in the forums there is a green up arrow beside the message that adds to the authors rep, and a red one which takes from it.

          If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          G 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            You're welcome! You can vote on (almost) any message - in the forums there is a green up arrow beside the message that adds to the authors rep, and a red one which takes from it.

            If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

            G Offline
            G Offline
            Goaty65109
            wrote on last edited by
            #9

            Question that came into my mind whilst I am close to being done rebuilding. Can I change the SearchOption.AllDirectories into something more specific? In case I copy and paste a directory to make a backup of the original settings? Like for instance, only look in folders that BEGIN with "BRD"? Thanks again. SearchOption = "\\BRD*"); <--- just an example of what I mean Edit: Ok it's all built. I am now running into an exception. Apparently, the name IniReadValue does not exist in the current context. But it exists in my ini class. public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); }

            OriginalGriffO 1 Reply Last reply
            0
            • G Goaty65109

              Question that came into my mind whilst I am close to being done rebuilding. Can I change the SearchOption.AllDirectories into something more specific? In case I copy and paste a directory to make a backup of the original settings? Like for instance, only look in folders that BEGIN with "BRD"? Thanks again. SearchOption = "\\BRD*"); <--- just an example of what I mean Edit: Ok it's all built. I am now running into an exception. Apparently, the name IniReadValue does not exist in the current context. But it exists in my ini class. public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); }

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #10

              No - the search only considers file names, it doesn't compare against the path. But it's pretty easy do check afterwards:

              if (file.Contains(@"\BRD"))
              {
              ...
              }

              Exception: (I suspect it's a compilation error?) Show your code (cut'n' paste so you don't mistype - and try using the "code" widget above the text box to format your code - it makes it easier to read)

              If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              G 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                No - the search only considers file names, it doesn't compare against the path. But it's pretty easy do check afterwards:

                if (file.Contains(@"\BRD"))
                {
                ...
                }

                Exception: (I suspect it's a compilation error?) Show your code (cut'n' paste so you don't mistype - and try using the "code" widget above the text box to format your code - it makes it easier to read)

                If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                G Offline
                G Offline
                Goaty65109
                wrote on last edited by
                #11

                Here is my main form:

                using System;
                using System.Collections;
                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 ReadWriteIni;
                using Ini;

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

                    private void Form1\_Load(object sender, EventArgs e)
                    {
                        string\[\] settingsFiles = Directory.GetFiles(@"C:\\\\Users\\\\Name\\\\Desktop\\\\Projects\\\\test.Files\\\\Folders\\\\",
                                "Settings.txt", SearchOption.AllDirectories);
                        comboInst.Items.Clear();
                        foreach (string file in settingsFiles)
                        {
                            IniFile ini = new IniFile(file);
                            comboInst.Items.Add(Ini.IniReadValue("Info", "Name"));
                        }
                    }
                }
                

                }

                And here is my Ini Class (Wish there was a spoiler tag to collapse this part...)

                using System;
                using System.IO;
                using System.Runtime.InteropServices;
                using System.Text;

                namespace Ini
                {
                public class IniFile
                {
                public string path;

                    \[DllImport("kernel32")\]
                    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
                    \[DllImport("kernel32")\]
                    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
                
                    public IniFile(string INIPath)
                    {
                        path = INIPath;
                    }
                    public void IniWriteValue(string Section, string Key, string Value)
                    {
                        WritePrivateProfileString(Section, Key, Value, this.path);
                    }
                    public string IniReadValue(string Section, string Key)
                    {
                        StringBuilder temp = new StringBuilder(255);
                        int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
                        return temp.ToString();
                
                    }
                }
                

                }

                OriginalGriffO 1 Reply Last reply
                0
                • G Goaty65109

                  Here is my main form:

                  using System;
                  using System.Collections;
                  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 ReadWriteIni;
                  using Ini;

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

                      private void Form1\_Load(object sender, EventArgs e)
                      {
                          string\[\] settingsFiles = Directory.GetFiles(@"C:\\\\Users\\\\Name\\\\Desktop\\\\Projects\\\\test.Files\\\\Folders\\\\",
                                  "Settings.txt", SearchOption.AllDirectories);
                          comboInst.Items.Clear();
                          foreach (string file in settingsFiles)
                          {
                              IniFile ini = new IniFile(file);
                              comboInst.Items.Add(Ini.IniReadValue("Info", "Name"));
                          }
                      }
                  }
                  

                  }

                  And here is my Ini Class (Wish there was a spoiler tag to collapse this part...)

                  using System;
                  using System.IO;
                  using System.Runtime.InteropServices;
                  using System.Text;

                  namespace Ini
                  {
                  public class IniFile
                  {
                  public string path;

                      \[DllImport("kernel32")\]
                      private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
                      \[DllImport("kernel32")\]
                      private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
                  
                      public IniFile(string INIPath)
                      {
                          path = INIPath;
                      }
                      public void IniWriteValue(string Section, string Key, string Value)
                      {
                          WritePrivateProfileString(Section, Key, Value, this.path);
                      }
                      public string IniReadValue(string Section, string Key)
                      {
                          StringBuilder temp = new StringBuilder(255);
                          int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
                          return temp.ToString();
                  
                      }
                  }
                  

                  }

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #12

                  Remember that c# is case sensitive:

                              IniFile ini = new IniFile(file);
                              comboInst.Items.Add(Ini.IniReadValue("Info", "Name"));
                  

                  ------------------------------------^

                  Becomes:

                              IniFile ini = new IniFile(file);
                              comboInst.Items.Add(ini.IniReadValue("Info", "Name"));
                  

                  ------------------------------------^

                  You need to make the two names match!

                  If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  G 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Remember that c# is case sensitive:

                                IniFile ini = new IniFile(file);
                                comboInst.Items.Add(Ini.IniReadValue("Info", "Name"));
                    

                    ------------------------------------^

                    Becomes:

                                IniFile ini = new IniFile(file);
                                comboInst.Items.Add(ini.IniReadValue("Info", "Name"));
                    

                    ------------------------------------^

                    You need to make the two names match!

                    If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                    G Offline
                    G Offline
                    Goaty65109
                    wrote on last edited by
                    #13

                    Well I'll be damned. It works! Words cannot express my thanks. I can now move on with the fun, visual side of things and actually get this mess put together. You sir are fantastic. Bookmarked, as well as a couple of your Guides (I'll probably be seeing you again once I wish to make simple SELECT queries to SQL). It's 6:15 here, been trying to tackle this since 9pm yesterday, you saved me so much time and I appreciate it. You have a good day man! and good night. :) :) :) :) :) :)

                    OriginalGriffO 1 Reply Last reply
                    0
                    • G Goaty65109

                      Well I'll be damned. It works! Words cannot express my thanks. I can now move on with the fun, visual side of things and actually get this mess put together. You sir are fantastic. Bookmarked, as well as a couple of your Guides (I'll probably be seeing you again once I wish to make simple SELECT queries to SQL). It's 6:15 here, been trying to tackle this since 9pm yesterday, you saved me so much time and I appreciate it. You have a good day man! and good night. :) :) :) :) :) :)

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #14

                      You're welcome!

                      If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups