Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can anyone give an example of how to read and write cookies in c# windows application.

plz help me..

dear folks... I have tried something like this..

I have added cookie using HttpWebResponse.
C#
        private void btnResponse_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.100/login/index.aspx");
            HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

            Cookie ckepath = new Cookie("cielopath", "c:");
            httpResponse.Cookies.Add(ckepath);
        }

but when i request(try to get the value)it its empty..i think its not added in system....plz help
        private void btnRequest_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.100/login/index.aspx");
            HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

            foreach (Cookie cke in httpResponse.Cookies)
            {
                string val = cke.ToString();
            }
        }
Posted
Updated 23-Nov-12 20:37pm
v3
Comments
Andy411 23-Nov-12 7:59am    
Do you realy mean "cookies" in a windows application and not an in ASP?
If so, maybe you can explain your problem more exactly, please. Do you have code with Session["foo"] inside it and you want to use it in a "normal" windows application?
rahulshek 23-Nov-12 23:29pm    
yes cookies in c# windows application.actually i want to store a directory path in it which is done in the same way in asp.net.
plz help me already i am in deadline
Andy411 24-Nov-12 1:36am    
If the path is user specific, you can take a look at app.config and Settings in MSDN and Codeproject.
Abhishek Pant 24-Nov-12 3:22am    
cookies doesn't exits in windows forms application;but in case in you are using a web browser control then you can use in that thing.you can save the data in a database.

Cookies do not exist in windows applications - instead data is either saved in files, or in application settings, or in the registry depending on the quantity of data and it's organisation. You might save it in a database, or an XML file, or as striaght binary info.

But you don't use cookies in windows apps - they are for setting on the client from the server and those concepts do not apply when using Windows apps.

Application settings are probably the easiest:
1) Click on the project in Solution Explorer, choose "Properties".
2) Double click "Settings.Settings".
3) In the resulting dialog, Create application settings by specifying the Name, Type and Value - leave the "Scope" as "User".
4) You can now save and load your values easily:
C#
Properties.Settings.Default.MySettingString = "New Value";
Properties.Settings.Default.Save();
...
string myValue = Properties.Settings.Default.MySettingString;
 
Share this answer
 
Hi Rahul,

Well AFAIK, cookies are for browsers. If you're using a web Browser control by any means inside your windows form, I think you may do it with the help of that control, but if you're on to a windows form application without any web browser control then there's no such cookie thing you can use. Try to store the keys/values in a plain or encrypted text form in some flat file if you want it to be persisted or just go with variables if you don't.


Hope it helps !
Happy Coding :)
 
Share this answer
 

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