user725110
user725110

Reputation: 50

How to read cookies4.dat file using c#?

I want to display the list of cookies, i am unable to read this file. can anyone please guide me on reading data from this file(cookies4.dat). It is from opera browser.

Thanks in Advance.

Upvotes: 0

Views: 1120

Answers (3)

C-Pound Guru
C-Pound Guru

Reputation: 16368

This CodeProject article shows details on reading cookies for the major browsers, including Opera. Unfortunately it doesn't give much details on how the magic is done but you should be able to download the code and check it out.

A couple of methods included:

private static string GetOperaCookiePath()
{
    string s = Environment.GetFolderPath(
        Environment.SpecialFolder.ApplicationData);
    s += @"\Opera\Opera\cookies4.dat";

    if (!File.Exists(s))
        return string.Empty;

    return s;
}

private static bool GetCookie_Opera(string strHost, string strField, ref string Value)
{
    Value = "";
    bool fRtn = false;
    string strPath;

    // Check to see if Opera Installed
    strPath = GetOperaCookiePath();
    if (string.Empty == strPath) // Nope, perhaps another browser
        return false;

    try
    {
        OpraCookieJar cookieJar = new OpraCookieJar(strPath);
        List<O4Cookie> cookies = cookieJar.GetCookies(strHost);

        if (null != cookies)
        {
            foreach (O4Cookie cookie in cookies)
            {
                if (cookie.Name.ToUpper().Equals(strField.ToUpper()))
                {
                    Value = cookie.Value;
                    fRtn = true;
                    break;
                }
            }
        }
     }
    catch (Exception)
    {
        Value = string.Empty;
        fRtn = false;
    }
    return fRtn;
}

Upvotes: 0

Forgotten Semicolon
Forgotten Semicolon

Reputation: 14110

Have you tried the documentation?

Upvotes: 1

Related Questions