public void AddCookie(Uri siteUri,HttpCookieCollection cookies)
        {
            // Since we are not using children in this type
            // of index, all nodes are parent and use / instead of the default
            // value from AbsolutePath from Uri.

            string folderName = siteUri.Authority;// + siteUri.Port;
            string uriAndPort = siteUri.Authority;// + ":" + siteUri.Port;
            string dir = cookiesFolder + @"\" + folderName;

            if ( !Directory.Exists(dir) )
            {
                Directory.CreateDirectory(dir);
            }

            // search index data for root existence
            CookieDiskInfo domainIndex = (CookieDiskInfo)cookieIndex[uriAndPort];

            // add / update new cookie to disk
            if ( domainIndex != null )
            {
                // get current cookie index
                CookieDiskInfo cookieInfo = (CookieDiskInfo)cookieIndex[uriAndPort + "/"];

                // add/update file to disk
                string filePath = UpdateCookieFile(dir,"/",cookies);

                if ( cookieInfo == null )
                {
                    CookieDiskInfo newCookieInfo = new CookieDiskInfo(filePath);

                    // add reference to Index data
                    cookieIndex.Add(uriAndPort + "/",newCookieInfo);
                }

            }
            else
            {
                // add as root node
                // add file to disk
                string filePath = UpdateCookieFile(dir,"/",cookies);

                CookieDiskInfo cookieInfo = new CookieDiskInfo(filePath);

                // add cookie reference to Index data
                cookieIndex.Add(uriAndPort + "/",cookieInfo);

                // add root folder reference to Index data
                cookieIndex.Add(uriAndPort,new CookieDiskInfo(""));
            }

            UpdateIndexData();
        }
        // TODO: Check for errors while updating
        private string UpdateCookieFile(string dirPath,string absPath, HttpCookieCollection data)
        {
            string newPath = absPath;
            if ( !File.Exists(newPath) )
            {
                newPath = GetFilePath(dirPath, newPath);
            }

            FileStream stm = null;
            if ( File.Exists(newPath) )
            {
                File.Delete(newPath);
                stm = File.Open(newPath,FileMode.CreateNew);
            }
            else
            {
                // create any directory inside path
                string[] dirs = absPath.Split('/');
                string newDir = dirPath + "\\";
                for (int i=1;i<dirs.Length;i++)
                {
                    if (i==(dirs.Length-1))
                        break; // exit for loop

                    newDir += dirs[i] + "\\";
                    if ( !Directory.Exists(newDir) )
                    {
                        Directory.CreateDirectory(newDir);
                    }
                }
                stm = File.Open(newPath,FileMode.CreateNew);
            }

            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stm,data);
            stm.Close();

            return newPath;
        }
        private HttpCookieCollection OpenCookieData(string path)
        {
            FileStream stm = null;
            HttpCookieCollection result = new HttpCookieCollection();
            if ( File.Exists(path) )
            {
                try
                {
                    stm = File.Open(path,FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    result = (HttpCookieCollection)bf.Deserialize(stm);
                    stm.Close();
                }
                catch
                {
                    throw;
                }

            }
            return result;
        }
 internal HttpCookieCollectionEnumerator(HttpCookieCollection enumerable)
 {
     innerEnumerator = enumerable.InnerHash.GetEnumerator();
 }
 public HttpCookieCollection(HttpCookieCollection original)
 {
     innerHash = new Hashtable (original.innerHash);
 }
        public HttpCookieCollection Clone()
        {
            HttpCookieCollection clone = new HttpCookieCollection();
            clone.innerHash = (Hashtable) innerHash.Clone();

            return clone;
        }
        public static HttpCookieCollection Synchronized(HttpCookieCollection nonSync)
        {
            HttpCookieCollection sync = new HttpCookieCollection();
            sync.innerHash = Hashtable.Synchronized(nonSync.innerHash);

            return sync;
        }