Click here to Skip to main content
13,049,263 members (68,029 online)
Rate this:
 
Please Sign up or sign in to 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.
        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 23-Nov-12 0:27am
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
Properties.Settings.Default.MySettingString = "New Value";
Properties.Settings.Default.Save();
...
string myValue = Properties.Settings.Default.MySettingString;
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100