Mega Code Archive

 
Categories / C# / WPF
 

Create and retrieve cookies from a Windows Presentation Foundation (WPF) application using SetCookie and GetCookie

<Page x:Class="CookiesSampleCSharp.HomePage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     WindowTitle="Cookies Sample">   <StackPanel>     <Label>Set Cookie Value:</Label>     <TextBox Name="setCookieValueTextBox">Cookie1=Value1</TextBox>     <Button HorizontalAlignment="Right" Name="setCookieButton" Click="setCookieButton_Click">Set Cookie</Button>     <Label>Get Cookie Value:</Label>     <TextBox Name="getCookieValueTextBox"></TextBox>     <Button HorizontalAlignment="Right" Name="getCookieButton" Click="getCookieButton_Click">Get Cookie</Button>   </StackPanel>    </Page> //File:Window.xaml.cs using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; using System.Windows.Navigation; namespace CookiesSampleCSharp {     public partial class HomePage : System.Windows.Controls.Page     {         public HomePage()         {             InitializeComponent();         }         void setCookieButton_Click(object sender, RoutedEventArgs e)         {             try             {                 Application.SetCookie(BrowserInteropHelper.Source, this.setCookieValueTextBox.Text);             }             catch (Win32Exception ex)             {                 MessageBox.Show(ex.Message + " (Native Error Code=" + ex.NativeErrorCode + ")");             }         }         void getCookieButton_Click(object sender, RoutedEventArgs e)         {             try             {                 this.getCookieValueTextBox.Text = Application.GetCookie(BrowserInteropHelper.Source);             }             catch (Win32Exception ex)             {                 MessageBox.Show(ex.Message + " (Native Error Code=" + ex.NativeErrorCode + ")");             }         }     } }