• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

f2calv / CasCap.Common / 16933690265

13 Aug 2025 09:48AM UTC coverage: 0.32%. Remained the same
16933690265

push

github

web-flow
F2calv/2025 08 updates2 (#220)

* NoWarn updates

* re-added netstandard2.0 target

0 of 352 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 4 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

3 of 585 relevant lines covered (0.51%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/CasCap.Common.Extensions/Extensions/IOExtensions.cs
1
using Microsoft.Extensions.Logging;
2

3
namespace CasCap.Extensions;
4

5
public static class IOExtensions
6
{
7
    private static readonly ILogger _logger = ApplicationLogging.CreateLogger(nameof(IOExtensions));
×
8

9
    public static string Extend(this string root, string folderOrFile)
10
    {
11
        var path = Path.Combine(root, folderOrFile);
×
12
        return path;
×
13
    }
14

15
    //public static string ExtendAndCreateDirectory(this string root, string folderOrFile)
16
    //{
17
    //    var directory = root.ExtendPath(Path.GetFullPath(folderOrFile));
18
    //    if (!Directory.Exists(directory))
19
    //        Directory.CreateDirectory(directory);
20
    //    return directory;
21
    //}
22

23
    public static void WriteAllBytes(this string path, byte[] bytes)
24
    {
25
        var dir = Path.GetDirectoryName(path);
×
26
        if (dir is not null)
×
27
        {
28
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
×
29
            File.WriteAllBytes(path, bytes);
×
30
        }
31
        else
32
            throw new Exception($"GetDirectoryName not possible for path '{path}'");
×
33
    }
34

35
    public static void WriteAllText(this string path, string str)
36
    {
37
        var dir = Path.GetDirectoryName(path);
×
38
        if (dir is not null)
×
39
        {
40
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
×
41
            File.WriteAllText(path, str);
×
42
        }
43
        else
44
            throw new Exception($"GetDirectoryName not possible for path '{path}'");
×
45
    }
46

47
    public static void AppendTextFile(this string path, string content)
48
    {
49
        var dir = Path.GetDirectoryName(path);
×
50
        if (dir is not null)
×
51
        {
52
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
×
53
            if (!File.Exists(path))
×
54
            {
55
                using var sw = File.CreateText(path);
×
56
                sw.WriteLine(content);
×
57
            }
58
            else
59
            {
60
                try
61
                {
62
                    using var sw = File.AppendText(path);
×
63
                    sw.WriteLine(content);
×
64
                }
×
65
                catch (Exception ex)
×
66
                {
NEW
67
                    _logger.LogError(ex, "{ClassName} append failed", nameof(IOExtensions));
×
68
                    throw;
×
69
                }
70
            }
71
        }
72
        else
73
            throw new Exception($"GetDirectoryName not possible for path '{path}'");
×
74
    }
×
75

76
    public static List<string> ReadTextFile(this string path)
77
    {
78
        var output = new List<string>(50000);
×
79
        if (File.Exists(path))
×
80
        {
81
            //var count = TotalLines(path);
82
            //output = new List<string>(count);
83
            foreach (var line in File.ReadLines(path))
×
84
            {
85
                if (string.IsNullOrWhiteSpace(line)) continue;
×
86
                output.Add(line);
×
87
            }
88
            //using (var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
89
            //{
90
            //    using (var stream = new StreamReader(fs))
91
            //    {
92
            //        while (true)
93
            //        {
94
            //            var line = stream.ReadLine();
95
            //            output.Add(line);
96
            //            if (line is null)
97
            //                break;
98
            //        }
99
            //    }
100
            //}
101
        }
102
        return output;
×
103
    }
104

105
    /// <summary>
106
    /// Does a Path.Combine() and creates the destination directory if not existing
107
    /// </summary>
108
    public static string GetLocalPath(this string basePath, string relativePath)
109
    {
110
        var path = basePath.Extend(relativePath);
×
111
        var dirName = Path.GetDirectoryName(path);
×
112
        if (dirName is not null && !directories.Contains(dirName) && !Directory.Exists(dirName))
×
113
        {
114
            //Debugger.Break();
115
            Directory.CreateDirectory(dirName);
×
116
            directories.Add(dirName);
×
117
        }
118
        return path;
×
119
    }
120

121
    static HashSet<string> directories { get; set; } = [];
×
122

123
    public static float CalculateFolderSize(this string folder)
124
    {
125
        var folderSize = 0.0f;
×
126
        try
127
        {
128
            if (!Directory.Exists(folder))
×
129
                return folderSize;
×
130
            else
131
            {
132
                try
133
                {
134
                    foreach (var file in Directory.GetFiles(folder))
×
135
                    {
136
                        if (File.Exists(file))
×
137
                        {
138
                            var finfo = new FileInfo(file);
×
139
                            folderSize += finfo.Length;
×
140
                        }
141
                    }
142
                    foreach (var dir in Directory.GetDirectories(folder))
×
143
                        folderSize += CalculateFolderSize(dir);
×
144
                }
×
145
                catch (NotSupportedException e)
×
146
                {
147
                    throw new Exception($"Unable to calculate folder size: {e.Message}");
×
148
                }
149
            }
150
        }
×
151
        catch (UnauthorizedAccessException e)
×
152
        {
153
            throw new Exception($"Unable to calculate folder size: {e.Message}");
×
154
        }
155
        return folderSize;
×
156
    }
×
157

158
    public static (int files, int directories) Deltree(this string folder)
159
    {
160
        var di = new DirectoryInfo(folder);
×
161
        var files = 0;
×
162
        foreach (var file in di.GetFiles())
×
163
        {
164
            file.Delete();
×
165
            files++;
×
166
        }
167
        var directories = 0;
×
168
        foreach (var dir in di.GetDirectories())
×
169
        {
170
            dir.Delete(true);
×
171
            directories++;
×
172
        }
173
        return (files, directories);
×
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc