hey guys. i get an error "Could not find a part of the path" for inputStream = File.OpenText(textBox1.Text).
The idea is to enter a folders path then in another textbox enter word to search.
all the files in the folder have to be searched then the word,filename and path are strung into texbox2.
any ideas at what to do || what im doing wrong? Thanks heaps

private void searchbutton_Click(object sender, EventArgs e)
        {
            String Word;
            string[] words = new string[3];
            char []seperator = {','};
            bool found = false;
            StreamReader inputStream;
            if (inputtextBox.Text == "")
            {
                MessageBox.Show("Please enter something to search in files", "Error");
                inputtextBox.Focus();
            }
            else
                try
            {
                string[] directories = Directory.GetDirectories(textBox1.Text);
                string[] files = Directory.GetFiles(textBox1.Text);
                inputStream = File.OpenText(textBox1.Text);//* error comes in here*\\

                Word = inputStream.ReadLine();
                    
                        while ((Word != null) && found == false)
                        {
                            words = Word.Split(seperator);
                            if (words[0].Trim() == inputtextBox.Text)
                            {
                              textBox2.Text = words[1].Trim();
                              textBox2.Text = words[2].Trim();
                              found = true;
                            }
                         }
                        inputStream.Close();

                for (int i = 0; i < files.Length; i++)
                {
      if (files[i].Contains(inputtextBox.Text))
      textBox2.Text += files[i].Substring(files[i].LastIndexOf('\\') + 1) + "\t" + files [i] + "\n";
                }

Recommended Answers

All 4 Replies

Your problem here is in parsing. Remember the special characters that ASCII uses, like \n for newline, or \\ for backslash? What the compiler tries to do here is read what the user input -for example, "C:\WINDOWS\OPTIONS\CABS"- and make sense of it. Do you see the problem yet? All special characters have that backslash in front of them, so when you really want to use just a backslash, you have to type \\ to clarify that.
For this code, you're going to have to parse the string in textBox1.text before you can use it to open your streamreader.

This should do the job, try to understand how I solved it, if you don't be sure to ask me!

private void button1_Click(object sender, EventArgs e)
        {
            string[] words;
            StreamReader inputStream;
            if ( textBox1.Text == "" )
            {
                MessageBox.Show("Please enter something to search in files", "Error");
                textBox1.Focus();
            }
            else
                try
                {
                    string[] directories = Directory.GetDirectories(textBox1.Text);
                    string[] files = Directory.GetFiles(textBox1.Text);
                    foreach ( string file in files )
                    {
                        if ( file.Contains(".txt") )
                        {
                            inputStream = File.OpenText(file);//* error comes in here*\\

                            words = inputStream.ReadLine().Split();
                            foreach ( string word in words )
                            {
                                if ( word == textBox2.Text ) listBox1.Items.Add(word + " - " + file);
                            }
                            inputStream.Close();
                        }
                    }

                } catch ( Exception ex )
                {
                    MessageBox.Show(ex.Message);
                }
        }

Thanks clonexpert, your amazing. I see where i went wrong in my translation i knew i had to change my loop but i wasnt sure how.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.