首页 新闻 会员 周边

写文件问题?

0
[已关闭问题] 关闭于 2013-01-09 15:07

我的这个代码,有问题么?

 1         FileStream cacheFileStream = null;
 2 
 3         try
 4         {
 5             if (!Directory.Exists(folderPath))
 6             {
 7                 Directory.CreateDirectory(folderPath);
 8             }
 9 
10             TimeSpan timeSpan = DateTime.Now - File.GetLastWriteTime(cacheFilePath);
11             if (timeSpan.TotalSeconds < runtimeCacheSeconds)
12             {
13                 context.Response.TransmitFile(cacheFileRelPath);
14             }
15             else
16             {
17 
18                 WebClient wc = new WebClient();
19                 byte[] pageData = wc.DownloadData(url);
20                 cacheFileStream = File.Create(cacheFilePath);
21 
22                 lock (cacheFileStream)
23                 {
24                     cacheFileStream.Write(pageData, 0, pageData.Length);                  
25                     context.Response.TransmitFile(cacheFileRelPath);
26                 }
27             }
28 
29         }
30         catch
31         {
32             if (File.Exists(cacheFilePath))
33             {
34                 context.Response.TransmitFile(cacheFilePath);
35             }
36         }
37         finally
38         {
39             if (cacheFileStream != null)
40             {
41                 cacheFileStream.Close();
42                 cacheFileStream.Dispose();
43             }
44         }
fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2012-05-17 16:41
< >
分享
所有回答(1)
0

有什么问题?

artwl | 园豆:16736 (专家六级) | 2012-05-17 21:34
 WebClient wc = new WebClient();
19                 byte[] pageData = wc.DownloadData(url);
20                 cacheFileStream = File.Create(cacheFilePath);
21 
22                 lock (cacheFileStream)
23                 {
24                     cacheFileStream.Write(pageData, 0, pageData.Length);                  
25                     context.Response.TransmitFile(cacheFileRelPath);
26                 }

 

我做了修改,为了防止并发创建文件时候重复的创建文件

不知道lock 的用法对不对

 

 1         FileStream cacheFileStream = null;
 2         bool cacheFileClose = false;
 3 
 4         try
 5         {
 6             if (!Directory.Exists(folderPath))
 7             {
 8                 Directory.CreateDirectory(folderPath);
 9             }
10 
11 
12             FileInfo cacheFileInfo = new FileInfo(cacheFilePath);
13 
14             lock (cacheFileInfo)
15             {
16                 if (cacheFileInfo.Exists)
17                 {
18                     TimeSpan timeSpan = DateTime.Now - cacheFileInfo.LastWriteTime;
19                     if (timeSpan.TotalSeconds < runtimeCacheSeconds)
20                     {
21                         context.Response.TransmitFile(cacheFileRelPath);
22                         return;
23                     }
24                 }
25 
26 
27                 WebClient wc = new WebClient();
28                 byte[] pageData = wc.DownloadData(url);
29 
30                 cacheFileStream = cacheFileInfo.Create();
31                 cacheFileStream.Write(pageData, 0, pageData.Length);
32                 cacheFileStream.Close();
33                 cacheFileStream.Dispose();
34                 cacheFileClose = true;
35                 context.Response.TransmitFile(cacheFileRelPath);
36 
37             }
38 
39         }
40         catch
41         {
42             if (File.Exists(cacheFilePath))
43             {
44                 context.Response.TransmitFile(cacheFilePath);
45             }
46         }
47         finally
48         {
49             if (cacheFileClose == false)
50             {
51                 cacheFileStream.Close();
52                 cacheFileStream.Dispose();
53             }
54         }

 

支持(0) 反对(0) fun5 | 园豆:4 (初学一级) | 2012-05-18 11:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册