Click here to Skip to main content
12,395,143 members (65,134 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C#
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("");
            HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
            Cookie ckepath = new Cookie("cielopath", "c:");
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("");
            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
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
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 can save the data in a database.
Rate this: bad
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 Smile | :)
Rate this: bad
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";
string myValue = Properties.Settings.Default.MySettingString;
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

hi rahul,

Who gave this type of task...
André Kraak 27-Nov-12 5:09am
Please do not post a solution for asking a question. If you have a question or comment use the "Have a Question or Comment?" option beneath the question.

Please move the content of this solution to "Question or Comment" section and remove the solution.
Thank you.

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.160721.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2016
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