获取Internet临时文件路径,通过正则匹配所有Cookie文件或者指定域名Cookie真实地址
应该使用Win7以上的,我Win7没有问题,可以拿到Cookie地址和其内容,然后进行Cookie转换
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CookieTest
{
public class FileComm
{
/// <summary>
/// Internet临时文件路径
/// </summary>
string cookieUrl;
public FileComm()
{
//Internet临时文件路径
cookieUrl = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
}
//用来把 FileTime时间格式转化成c#中的string类型,以便我们进一步操作。
//主体程序如下:
#region 引入dll
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct INTERNET_CACHE_ENTRY_INFO
{
public int dwStructSize;
public IntPtr lpszSourceUrlName;
public IntPtr lpszLocalFileName;
public int CacheEntryType;
public int dwUseCount;
public int dwHitRate;
public int dwSizeLow;
public int dwSizeHigh;
public FILETIME LastModifiedTime;
public FILETIME ExpireTime;
public FILETIME LastAccessTime;
public FILETIME LastSyncTime;
public IntPtr lpHeaderInfo;
public int dwHeaderInfoSize;
public IntPtr lpszFileExtension;
public int dwExemptDelta;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int FileTimeToSystemTime(
IntPtr lpFileTime,
IntPtr lpSystemTime);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindFirstUrlCacheEntry(
[MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
IntPtr lpFirstCacheEntryInfo,
ref int lpdwFirstCacheEntryInfoBufferSize);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool FindNextUrlCacheEntry(
IntPtr hEnumHandle,
IntPtr lpNextCacheEntryInfo,
ref int lpdwNextCacheEntryInfoBufferSize);
[DllImport("wininet.dll")]
public static extern bool FindCloseUrlCache(
IntPtr hEnumHandle);
const int ERROR_NO_MORE_ITEMS = 259;
#endregion
#region FileTimeToSystemTime
private string FILETIMEtoDataTime(FILETIME time)
{
IntPtr filetime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FILETIME)));
IntPtr systime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SYSTEMTIME)));
Marshal.StructureToPtr(time, filetime, true);
FileTimeToSystemTime(filetime, systime);
SYSTEMTIME st = (SYSTEMTIME)Marshal.PtrToStructure(systime, typeof(SYSTEMTIME));
string Time = st.wYear.ToString() + "." + st.wMonth.ToString() + "." + st.wDay.ToString() + "." + st.wHour.ToString() + "." + st.wMinute.ToString() + "." + st.wSecond.ToString();
return Time;
}
#endregion
/// <summary>
/// 获取临时文件
/// </summary>
/// <param name="url">文件目录</param>
/// <param name="regex">筛选正则(临时文件名)</param>
/// <returns>键为临时文件名,值为实际文件地址</returns>
public Dictionary<string, string> GetFiles(string url, Regex regex = null)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
int nNeeded = 0, nBufSize;
IntPtr buf;
INTERNET_CACHE_ENTRY_INFO CacheItem;
IntPtr hEnum;
bool r;
FindFirstUrlCacheEntry(url, IntPtr.Zero, ref nNeeded);
if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
return dic;
nBufSize = nNeeded;
buf = Marshal.AllocHGlobal(nBufSize);
hEnum = FindFirstUrlCacheEntry(null, buf, ref nNeeded);
while (true)
{
CacheItem = (INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buf,
typeof(INTERNET_CACHE_ENTRY_INFO));
string modifiedTime = FILETIMEtoDataTime(CacheItem.LastModifiedTime);
string expireTime = FILETIMEtoDataTime(CacheItem.ExpireTime);
string accessTime = FILETIMEtoDataTime(CacheItem.LastAccessTime);
string syncTime = FILETIMEtoDataTime(CacheItem.LastSyncTime);
#region 获得数据,存入数据库
try
{
//此处遍历CacheItem即可
//例如
string sname = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName); //蛇皮名称,不知道怎么说,非文件实际名称
string w = Marshal.PtrToStringAuto(CacheItem.lpszLocalFileName);//真实文件地址
if (regex == null)
{
dic[sname] = w;
}
else if (regex.IsMatch(sname))
{
dic[sname] = w;
}
}
catch
{
//异常处理
}
#endregion
string s = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName);
nNeeded = nBufSize;
r = FindNextUrlCacheEntry(hEnum, buf, ref nNeeded);
if (!r && Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
break;
if (!r && nNeeded > nBufSize)
{
nBufSize = nNeeded;
buf = Marshal.ReAllocHGlobal(buf, (IntPtr)nBufSize);
FindNextUrlCacheEntry(hEnum, buf, ref nNeeded);
}
}
Marshal.FreeHGlobal(buf);
return dic;
}
/// <summary>
/// 获取指定域名的Cookie文件
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public Dictionary<string, string> GetCookieFile(string url = null)
{
//默认获取所有Cookie文件
Regex r = new Regex("^Cookie:administrator@");
if (url != null)
{
r = new Regex("^Cookie:administrator@" + url + "/$");
}
return GetFiles(cookieUrl, r);
}
}
}
我的博客地址:Wy博客