Lean  $LEAN_TAG$
TemporaryPathProvider.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Threading.Tasks;
20 using QuantConnect.Util;
21 
22 namespace QuantConnect.ToolBox
23 {
24  /// <summary>
25  /// Helper method that provides and cleans given temporary paths
26  /// </summary>
27  public static class TemporaryPathProvider
28  {
29  private static readonly Queue<string> TemporaryPaths = new Queue<string>();
30 
31  // Gets a new temporary path
32  public static string Get()
33  {
34  var newPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToStringInvariant(null));
35  lock (TemporaryPaths)
36  {
37  TemporaryPaths.Enqueue(newPath);
38  }
39  return newPath;
40  }
41 
42  /// <summary>
43  /// Recursively deletes all the given temporary paths
44  /// </summary>
45  public static void Delete()
46  {
47  List<string> paths;
48  lock (TemporaryPaths)
49  {
50  paths = TemporaryPaths.ToList(s => s);
51  TemporaryPaths.Clear();
52  }
53  Parallel.ForEach(paths, path =>
54  {
55  try
56  {
57  Directory.Delete(path, recursive: true);
58  }
59  catch
60  {
61  // pass
62  }
63  });
64  }
65  }
66 }