This repository was archived by the owner on Feb 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateCacheManager.cs
More file actions
117 lines (101 loc) · 4.42 KB
/
Copy pathCreateCacheManager.cs
File metadata and controls
117 lines (101 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace PowerCacheOffice
{
internal class CreateCacheManager
{
public int CacheTargetCount { get; private set; }
public int CreatedCacheCount { get; private set; }
private static readonly string powerCacheOfficeDataFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"PowerCacheOffice");
private static readonly string powerCacheOfficeCacheFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"PowerCacheOffice\.cache");
private List<string> caches = new List<string>();
public CreateCacheManager()
{
CacheTargetCount = 0;
CreatedCacheCount = 0;
}
public Task CountCacheTargetAsync(string folder)
{
return Task.Run(() =>
{
CountCacheTarget(folder);
});
}
private void CountCacheTarget(string folder)
{
if (folder.ToLower().StartsWith(powerCacheOfficeDataFolder.ToLower())) return;
foreach (var x in Directory.EnumerateFiles(folder))
{
try
{
if ((new FileInfo(x).Attributes & FileAttributes.System) == FileAttributes.System) continue;
var extension = Path.GetExtension(x).ToLower();
if (extension != ".xls" && extension != ".xlsx" && extension != ".xlsm" &&
extension != ".doc" && extension != ".docx" && extension != ".docm" &&
extension != ".ppt" && extension != ".pptx" && extension != ".pptm") continue;
CacheTargetCount++;
}
catch { }
}
foreach (var x in Directory.EnumerateDirectories(folder))
{
try
{
if ((new DirectoryInfo(x).Attributes & FileAttributes.System) == FileAttributes.System) continue;
CountCacheTarget(x);
}
catch { }
}
}
public Task CreateCacheAsync(string folder, List<string> caches)
{
this.caches = caches;
return Task.Run(() =>
{
CreateCache(folder);
});
}
private void CreateCache(string folder)
{
if (folder.ToLower().StartsWith(powerCacheOfficeDataFolder.ToLower())) return;
foreach (var x in Directory.EnumerateFiles(folder))
{
try
{
if ((new FileInfo(x).Attributes & FileAttributes.System) == FileAttributes.System) continue;
var extension = Path.GetExtension(x).ToLower();
if (extension != ".xls" && extension != ".xlsx" && extension != ".xlsm" &&
extension != ".doc" && extension != ".docx" && extension != ".docm" &&
extension != ".ppt" && extension != ".pptx" && extension != ".pptm") continue;
try
{
if (caches.Any(cache => cache == x)) continue;
var itemCacheFolder = Path.Combine(powerCacheOfficeCacheFolder, Guid.NewGuid().ToString());
if (!Directory.Exists(itemCacheFolder)) Directory.CreateDirectory(itemCacheFolder);
var cacheFile = Path.Combine(itemCacheFolder, Path.GetFileName(x));
Program.CopyFileAndAttributes(x, cacheFile);
File.AppendAllText(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"PowerCacheOffice\.createdCacheList.txt"),
x + "\t" + cacheFile + "\t" + File.GetLastWriteTime(x).ToString() + "\n");
}
finally { CreatedCacheCount++; }
}
catch { }
}
foreach (var x in Directory.EnumerateDirectories(folder))
{
try
{
if ((new DirectoryInfo(x).Attributes & FileAttributes.System) == FileAttributes.System) continue;
CreateCache(x);
}
catch { }
}
}
}
}