chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Linq;
using System.IO;
using System.Diagnostics;
 
namespace OpenTap
{
    /// <summary>
    /// Utility class to help with common file system operations.
    /// </summary>
    class FileSystemHelper
    {
        static void deleteAllFiles(string target_dir)
        {
            foreach (string file in Directory.GetFiles(target_dir))
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }
        }
 
        static void deleteAllDirectories(string target_dir)
        {
            string[] dirs = Directory.GetDirectories(target_dir);
 
            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }
        }
        /// <summary>
        /// Deletes a directory with files.
        /// </summary>
        /// <param name="target_dir"></param>
        public static void DeleteDirectory(string target_dir)
        {
            if (target_dir == null)
                throw new ArgumentNullException("target_dir");
            try
            {
                deleteAllFiles(target_dir);
                deleteAllDirectories(target_dir);
                Directory.Delete(target_dir, false);
            }
            catch (Exception)
            {
 
            }
        }
 
        public static void SafeDelete(string file, int retries, Action<int, Exception> onError)
        {
            for(int i = 0; i < retries; i++)
            {
                try
                {
                    File.Delete(file);
                    break;
                }
                catch(Exception e)
                {
                    if (e is DirectoryNotFoundException)
                    {
                        // this occurs if the directory of the file being deleted does not exist.
                        // But if the directory is not found it also means that the file does not exist.
                        // so we can safely assume it is deleted.
                        break;
                    }
                    onError(i, e);
                }
            }
        }
 
        /// <summary>
        /// Creates a directory if it does not already exist.
        /// </summary>
        /// <param name="filePath"></param>
        public static void EnsureDirectoryOf(string filePath)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filePath)) && string.IsNullOrWhiteSpace(Path.GetDirectoryName(filePath)) == false)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            }
        }
 
        /// <summary>
        /// Creates a temporary directory.
        /// </summary>
        /// <returns> Path to the temporary directory.</returns>
        public static string CreateTempDirectory()
        {
            string path = Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName());
            EnsureDirectoryOf(path);
            return path;
        }
 
        public static string CreateTempFile(string extension)
        {
            return Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName()) + extension;
        }
 
        public static string GetCurrentInstallationDirectory()
        {
            return ExecutorClient.ExeDir;
        }
 
        /// <summary>
        /// Compares two paths to get the relative between base and end. The string has to be a standard file system string like "C:\Program Files\...".
        /// </summary>
        /// <param name="baseDirectory"></param>
        /// <param name="endDirectory"></param>
        /// <returns></returns>
        internal static string GetRelativePath(string baseDirectory, string endDirectory)
        {
            var baseSplit = baseDirectory.Split('\\');
            var endSplit = endDirectory.Split('\\');
            int idx = getSameIndex(baseSplit, endSplit);
            var dots = String.Join("\\", baseSplit.Skip(idx).Select(v => ".."));
            var afterDots = string.Join("\\", endSplit.Skip(idx));
 
            string temp = System.IO.Path.Combine(dots, afterDots);
 
            //If the resulting directory is empty, put in a dot.  This allows the hover help to work
            return temp == string.Empty ? "." : temp;
        }
 
        static int getSameIndex(string[] a, string[] b)
        {
            int end = Math.Min(a.Length, b.Length);
            for (int i = 0; i < end; i++)
            {
                if (a[i] != b[i])
                {
                    return i;
                }
            }
            return end;
        }
 
        public static string GetAssemblyVersion(string assemblyPath)
        {
            if (assemblyPath == null)
                throw new ArgumentNullException("assemblyPath");
            assemblyPath = Path.GetFullPath(assemblyPath); // this is important to make sure that we take the version number from the file in the current directory, and not the one in TAP_PATH
            FileVersionInfo info = FileVersionInfo.GetVersionInfo(assemblyPath);
            return info.ProductVersion;
        }
    }
}