Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use code for the search file



C#
using System;
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 System.Configuration;
using System.Text;


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

        private void button1_Click(object sender, EventArgs e)
        {
            {
                string search = textBox1.Text;
                string[] files = Directory.GetFiles(@"D:\\dhaval\\study", "*.*", SearchOption.AllDirectories);
                int Flag = 0;
                string dir = @"D:\\dhaval\\study";
                string[] files1;
                int numFiles;
                files1 = Directory.GetFiles(dir);
                numFiles = files.Length;
                Response.Write("Files searched : " + numFiles + "<br>");
                for (int i = 0; i < numFiles; i++)
                {


                    string file = files[i].ToString();

                    int file1 = file.IndexOf(search, StringComparison.CurrentCultureIgnoreCase);
                    if (file1 != -1)
                    {
                        Response.Write("<br>" + file);
                        //Response.Write("<div style='removed:absolute;removed" + file + "px;removed" +  "px'>"+file! + "</div>");
                        Flag = 1;

                    }




                }
                if (Flag == 0)
                {
                    Response.Write("No Matches Found");
                }
            }
        }
    }
}


so i get following error

Error :"The name 'Response' does not exist in the current context"
so how to remove this error please give me suggestion
thanks
Posted
Updated 28-Jan-14 5:49am
v2

Clearly your are using Windows Forms. So in this context there is no Response object, which you can only find in web applications.

So you have several choices :

1- Create a web application instead of a Windows Forms one

2- Create a console application and use
C#
Console.Write("Whatever text you want to present to the user");


3- Keep your Windows Forms Application and use a control (for example, a TextBox) to present text messages to the user.
You can also use the Debug object, but in this case message will not be presented on the application itself but in the debug console of your IDE.

But the main useful advise would be: please learn to study the objects you are using. Here it seems you have copied/pasted some codes obtained from several sources without understanding what is behind them in the first place.
You should study the differences between a Windows Forms Application and an ASP.NET Web Application.
Some useful links:
Choosing Between Windows Forms and Web Forms[^]
ASP.NET Verses Winforms[^]

These are just the most obvious links; you can find many others using your favorite search engine.

Good luck, learning development takes some time, don't be afraid to read, and read, and then read some more.
 
Share this answer
 
You have copied the code from some web application and pasted in the Windows application.

To view the output in the screen
Create a label control ( Drag and drop a label ( default name:label1 ) to the form )

just replace these lines

C#
  Response.Write("<br>" + file);
  label1.Text += file + Environment.NewLine;


  Response.Write("No Matches Found");
  label1.Text += file + Environment.NewLine;
 
Share this answer
 
You have created a Windows Forms application but Response.Write does not exist in Windows Forms. See http://msdn.microsoft.com/en-us/library/ms525585(v=vs.90).aspx[^].
 
Share this answer
 
Comments
Rahul VB 27-Jan-14 13:34pm    
I dont understand why has this answer been down voted?
Richard MacCutchan 28-Jan-14 4:08am    
There are downvoters everywhere :( Thanks for countervote.
Rahul VB 28-Jan-14 4:13am    
Sir, the problem with students of today is they want spoon feeding. Their attitude is to simply post a gibberish paragraph and expect a coded solution. No one wants to take efforts.
Richard MacCutchan 28-Jan-14 7:07am    
Sad but true. And my name is Richard, my father was "Sir". :)
Rahul VB 28-Jan-14 12:02pm    
Ok Richard :)
The error message is clear: you didn't define any object Response. What is it supposed to be?
 
Share this answer
 
Comments
dhaval082 27-Jan-14 4:10am    
how to define object Response
Cutting all complexities,

Please create a web application and run the code, what you have done is created a winform application.
 
Share this answer
 
please add system.web dll and use the following code

C#
using System;
using System.Web;

using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

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

        private void button1_Click(object sender, EventArgs e)
        {
            {
                string search = "";
                string[] files = Directory.GetFiles(@"D:\\dhaval\\study", "*.*", SearchOption.AllDirectories);
                int Flag = 0;
                string dir = @"D:\\dhaval\\study";
                string[] files1;
                int numFiles;
                files1 = Directory.GetFiles(dir);
                numFiles = files.Length;
             
          
                HttpContext c = HttpContext.Current;      // added by muntaser zalloum 

                c.Response.Write("Files searched : " + numFiles + "<br>");


                for (int i = 0; i < numFiles; i++)
                {


                    string file = files[i].ToString();

                    int file1 = file.IndexOf(search, StringComparison.CurrentCultureIgnoreCase);
                    if (file1 != -1)
                    {
                      c. Response.Write("<br>" + file);
                        //Response.Write("<div style="removed:absolute;removed" + file + "px;removed" +  "px">"+file! + "</div>");
                        Flag = 1;

                    }

                }
                if (Flag == 0)
                {
                  c.  Response.Write("No Matches Found");
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 28-Jan-14 4:07am    
Please format your code and put <pre> tags around it.
CHill60 28-Jan-14 12:20pm    
formatted post but see other posts above for the correct solution(s)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900