首页 新闻 会员 周边

关于io流防问系统目录的权限问题??

0
悬赏园豆:10 [已关闭问题]

//本人想做一个通过io流来读取  C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files 下的所有临时文件的信息。

用的两种方法。1.通过io读取,可权限问题解决不了。

2.通过DirectoryInfo FileInfo两个类,可不能扫描到临时文件。

---------不知道该怎么做好了???/

protected void Page_Load(object sender, EventArgs e)
    {
        FileStream fiel = new FileStream(@"C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files", FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader read = new StreamReader(fiel);
        string st = read.ReadToEnd();
       Response.Write(st);
    }
   

问题补充: 我是说通过读取文件属性来排序。就像本身一样。有(名称,更新时间,最后防问时间等等 --------------- http://home.leefuu.com/attachment/201003/3/21658_1267618291GrCJ.jpg ---如果能像上面firefox 一样就好了。
window5549-accp的主页 window5549-accp | 初学一级 | 园豆:117
提问于:2010-03-02 15:41
< >
分享
其他回答(5)
0

这里的确存在权限的问题,需要给代码里指定的目录加上权限

如果是XP,需要给Asp.net加上访问权限

如果是Win2003,需要给Network Service加上访问权限

查尔斯 | 园豆:3832 (老鸟四级) | 2010-03-02 15:45
如果要读取系统目录,不光要加上权限,Temporary Internet Files这个目录与其他目录不同,需要调用Windows的API获取文件信息,可参考楼下的网址
支持(0) 反对(0) 查尔斯 | 园豆:3832 (老鸟四级) | 2010-03-02 15:58
@查尔斯:你的说加权限,好像不能加哦。Temporary Internet Files是设不成的。不知你具体指哪里?
支持(0) 反对(0) window5549-accp | 园豆:117 (初学一级) | 2010-03-03 09:12
0

看下这篇文章吧,  C# 获取 IE 临时文件 。http://blog.csdn.net/21aspnet/archive/2007/03/24/1539828.aspx

这里有详细的介绍,应该可以满足你的需要

西越泽 | 园豆:10775 (专家六级) | 2010-03-02 15:50
是做出来了一部分,可以读取文件,但不知对文件属性该如何做。我是说通过读取文件属性来排序。就像本身一样。有(名称,更新时间,最后防问时间等等
支持(0) 反对(0) window5549-accp | 园豆:117 (初学一级) | 2010-03-03 13:22
0

关注

woody.wu | 园豆:3621 (老鸟四级) | 2010-03-02 17:22
是做出来了一部分,可以读取文件,但不知对文件属性该如何做。我是说通过读取文件属性来排序。就像本身一样。有(名称,更新时间,最后防问时间等等)
支持(0) 反对(0) window5549-accp | 园豆:117 (初学一级) | 2010-03-03 13:22
0

大家知道,在我们访问一个网站的时候。系统会把这个网站上的图片,动画等内容全部缓存到Internet临时文件夹中。
我们可以通过 <Drives>:\Documents and Settings\<user>\Local Settings\Temporary Internet Files访问。但是可能我们都没有想到,里面的文件实际却不同于我们系统中其他的文件夹和文件的关系。

举例说明,我们在VS.net下写一个函数来返回指定文件夹中的文件夹和所有文件时,但我们把Internet临时文件夹的地址传进去时,系统只会返回一个文件,那就是desktop.ini(每个文件夹都有),还有一个隐藏的文件夹。所以这就证明了在临时文件夹中的文件并不是按照普通的文件夹与文件的方式存在的。

其实windows是把临时文件全部存在一个隐藏的文件夹中,这个文件夹是我们都看不到的,然后靠一个index.dat的索引把内容全部读出来回显给用户。

那我们怎么用程序来读取其中的内容呢? 
首先要引用一个user.dll,在系统文件夹中。然后利用它其中的一些函数就可以遍历整个文件夹,并获得其中每个文件的信息。

[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);


引入以上三个函数来遍历临时文件夹,然后再引用

[DllImport("kernel32.dll",SetLastError=true, CharSet=CharSet.Auto)]
public static extern int FileTimeToSystemTime(
IntPtr lpFileTime,
IntPtr lpSystemTime);

用来把 FileTime时间格式转化成c#中的string类型,以便我们进一步操作。

主体程序如下:

代码
1 #region 引入dll
2
3 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
4  public struct INTERNET_CACHE_ENTRY_INFO
5 {
6 public int dwStructSize;
7 public IntPtr lpszSourceUrlName;
8 public IntPtr lpszLocalFileName;
9 public int CacheEntryType;
10 public int dwUseCount;
11 public int dwHitRate;
12 public int dwSizeLow;
13 public int dwSizeHigh;
14 public FILETIME LastModifiedTime;
15 public FILETIME ExpireTime;
16 public FILETIME LastAccessTime;
17 public FILETIME LastSyncTime;
18 public IntPtr lpHeaderInfo;
19 public int dwHeaderInfoSize;
20 public IntPtr lpszFileExtension;
21 public int dwExemptDelta;
22 }
23
24 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
25 public struct SYSTEMTIME
26 {
27 public short wYear;
28 public short wMonth;
29 public short wDayOfWeek;
30 public short wDay;
31 public short wHour;
32 public short wMinute;
33 public short wSecond;
34 public short wMilliseconds;
35 }
36
37 [DllImport("kernel32.dll",SetLastError=true, CharSet=CharSet.Auto)]
38 public static extern int FileTimeToSystemTime(
39 IntPtr lpFileTime,
40 IntPtr lpSystemTime);
41
42 [DllImport("wininet.dll", SetLastError=true, CharSet=CharSet.Auto)]
43 public static extern IntPtr FindFirstUrlCacheEntry(
44 [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
45 IntPtr lpFirstCacheEntryInfo,
46 ref int lpdwFirstCacheEntryInfoBufferSize);
47
48 [DllImport("wininet.dll", SetLastError=true, CharSet=CharSet.Auto)]
49 public static extern bool FindNextUrlCacheEntry(
50 IntPtr hEnumHandle,
51 IntPtr lpNextCacheEntryInfo,
52 ref int lpdwNextCacheEntryInfoBufferSize);
53
54 [DllImport("wininet.dll")]
55 public static extern bool FindCloseUrlCache(
56 IntPtr hEnumHandle);
57
58 const int ERROR_NO_MORE_ITEMS = 259;
59
60 #endregion
61
62 #region FileTimeToSystemTime
63
64 private string FILETIMEtoDataTime(FILETIME time)
65 {
66 IntPtr filetime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(FILETIME)) );
67 IntPtr systime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(SYSTEMTIME)) );
68 Marshal.StructureToPtr(time,filetime,true);
69 FileTimeToSystemTime( filetime ,systime);
70 SYSTEMTIME st = (SYSTEMTIME) Marshal.PtrToStructure(systime,typeof(SYSTEMTIME));
71 string Time = st.wYear.ToString()+"."+st.wMonth.ToString()+"."+st.wDay.ToString()+"."+st.wHour.ToString()+"."+st.wMinute.ToString()+"."+st.wSecond.ToString();
72 return Time;
73 }
74
75 #endregion
76
77 #region 加载数据
78 private void FileOk_Click(object sender, System.EventArgs e)
79 {
80
81 int nNeeded = 0, nBufSize;
82 IntPtr buf;
83 INTERNET_CACHE_ENTRY_INFO CacheItem;
84 IntPtr hEnum;
85 bool r;
86
87 FindFirstUrlCacheEntry( null, IntPtr.Zero, ref nNeeded );
88
89 if ( Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS )
90 return;
91
92 nBufSize = nNeeded;
93 buf = Marshal.AllocHGlobal( nBufSize );
94 hEnum = FindFirstUrlCacheEntry( null, buf, ref nNeeded );
95 while ( true )
96 {
97 CacheItem = (INTERNET_CACHE_ENTRY_INFO) Marshal.PtrToStructure( buf,
98 typeof(INTERNET_CACHE_ENTRY_INFO) );
99
100 string modifiedTime = FILETIMEtoDataTime(CacheItem.LastModifiedTime);
101 string expireTime = FILETIMEtoDataTime(CacheItem.ExpireTime);
102 string accessTime = FILETIMEtoDataTime(CacheItem.LastAccessTime);
103 string syncTime = FILETIMEtoDataTime(CacheItem.LastSyncTime);
104
105 #region 获得数据,存入数据库
106 try
107 {
108
109 //此處遍歷CacheItem即可
110 //例如
111 string s = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName);
112 }
113 catch
114 {
115 //異常處理
116 }
117 #endregion
118
119 string s = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName);
120
121 nNeeded = nBufSize;
122 r = FindNextUrlCacheEntry( hEnum, buf, ref nNeeded );
123
124 if ( !r && Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS )
125 break;
126
127 if ( !r && nNeeded > nBufSize )
128 {
129 nBufSize = nNeeded;
130 buf = Marshal.ReAllocHGlobal( buf, (IntPtr) nBufSize );
131 FindNextUrlCacheEntry( hEnum, buf, ref nNeeded );
132 }
133 }
134
135 MessageBox.Show("系统数据加载完毕!");
136 Marshal.FreeHGlobal( buf );
137
138 }
139
140 #endregion
风影极光 | 园豆:1573 (小虾三级) | 2010-03-02 17:39
0

难道这么特殊?学习了!

Devin Mao | 园豆:596 (小虾三级) | 2010-03-02 18:01
0

尝试用楼上说的给对应的目录加权限试试看!!看能否解决你的问题。

langford | 园豆:710 (小虾三级) | 2010-03-02 18:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册