none
Recognize a folder Winforms RRS feed

  • Question

  • Hey!

    I need som help right now..
    Im making a program that looks like this.


    If you write a name in the second textbox and click "SkapaMapp" then it will create a folder with the Textbox as the name and a number like this:


    The numbers are a counter and will be higher for every folder that exist there.
    I have tryied to make so if you write "Test" again in the textbox then i want to look if the folder alredy exists and if it exists i want a messagebox to say "Hello i found "Test"". Here is my code but i dont get it right.. As i said above, it can be higher and lower numbers but it is allways a number then a "." and then the name..
    Any ideas how to solve this?
    Here is my code:

                KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1);
                string antal = "";
                foreach (string d in System.IO.Directory.GetDirectories(KundLänk))
                {
                    DirectoryInfo dir = new DirectoryInfo(d);
                    string dirName = dir.Name;
    
                    if (dirName != "Standard_Uppsättning")
                    {
                        Antal = dirName.Substring(dirName.IndexOf("."));
                    }
    
    
                    if (Antal == "." + KundNamnTxt)
                    {
                        //Antal = dirName.Substring(0, dirName.IndexOf("."));
                        // int test = Convert.ToInt32(Antal);
                        //DirectoryCount.Add(Convert.ToInt32(Antal));
                        //DirectoryCount.Sort();
                        KundMapp = dirName;
    
                    }
    Just ask if you have any questions!
    Thanks for the help! 
    Best regards!
    Thursday, May 28, 2020 5:32 PM

Answers

  • The problem is here:
            private void KundNamnTxt_TextChanged(object sender, EventArgs e)
            {
                KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1);
                string antal = "";
                foreach (string d in System.IO.Directory.GetDirectories(KundLänk))
                {
                    DirectoryInfo dir = new DirectoryInfo(d);
                    string dirName = dir.Name;
    
                    if (dirName != "Standard_Uppsättning")
                    {
                        HERE//Antal = dirName.Substring(dirName.IndexOf("."));
                    }
    
    
                    if (Antal == "." + KundNamnTxt)
                    {
                        //Antal = dirName.Substring(0, dirName.IndexOf("."));
                        // int test = Convert.ToInt32(Antal);
                        //DirectoryCount.Add(Convert.ToInt32(Antal));
                        //DirectoryCount.Sort();
                        KundMapp = dirName;
                        MessageBox.Show(dirName);
    
                    }
    
                }
    
    
    
                if (Directory.Exists(KundMapp))
                {
                    LaddaHittaErsätt.Visible = true;
                }
                else
                {
                    LaddaHittaErsätt.Visible = false;
                    HittaLbl.Visible = false;
                    ErsättLbl.Visible = false;
                    CreateBtn.Visible = false;
                    ClearControl(thisForm.Controls);
                }
    
                DirectoryCount.Clear();
            }
    It gets errors because the textbox text dosent have a "." in it. I want to just write the name and match it with a folder..

    • Marked as answer by Carlo Goretti Friday, May 29, 2020 11:55 AM
    Friday, May 29, 2020 6:17 AM

All replies

  • Greetings Carlo.

    I have tried your code, and it looks to me like it should work, provided the folder name in the TextBox is correct.

    The one thing I can think of that might be wrong is if the folder is "18.test" and the TextBox has "Test", they won't match because the casing is different (lower "t" and upper "T"). A simple way to fix this would be to make both strings the same case when you test.

    if(Antal.ToUpper() == ("." + KundNamn.Txt.ToUpper())  // ToLower would also work.
    Friday, May 29, 2020 2:18 AM
  • The problem is here:
            private void KundNamnTxt_TextChanged(object sender, EventArgs e)
            {
                KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1);
                string antal = "";
                foreach (string d in System.IO.Directory.GetDirectories(KundLänk))
                {
                    DirectoryInfo dir = new DirectoryInfo(d);
                    string dirName = dir.Name;
    
                    if (dirName != "Standard_Uppsättning")
                    {
                        HERE//Antal = dirName.Substring(dirName.IndexOf("."));
                    }
    
    
                    if (Antal == "." + KundNamnTxt)
                    {
                        //Antal = dirName.Substring(0, dirName.IndexOf("."));
                        // int test = Convert.ToInt32(Antal);
                        //DirectoryCount.Add(Convert.ToInt32(Antal));
                        //DirectoryCount.Sort();
                        KundMapp = dirName;
                        MessageBox.Show(dirName);
    
                    }
    
                }
    
    
    
                if (Directory.Exists(KundMapp))
                {
                    LaddaHittaErsätt.Visible = true;
                }
                else
                {
                    LaddaHittaErsätt.Visible = false;
                    HittaLbl.Visible = false;
                    ErsättLbl.Visible = false;
                    CreateBtn.Visible = false;
                    ClearControl(thisForm.Controls);
                }
    
                DirectoryCount.Clear();
            }
    It gets errors because the textbox text dosent have a "." in it. I want to just write the name and match it with a folder..

    • Marked as answer by Carlo Goretti Friday, May 29, 2020 11:55 AM
    Friday, May 29, 2020 6:17 AM