-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileNameExtract.cs
70 lines (58 loc) · 2.71 KB
/
FileNameExtract.cs
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
using Microsoft.SqlServer.Server;
using System.IO;
using System.Linq;
namespace MySQLCLRFunctions
{
public static class FileNameExtract
{
/*******************************************************************************************************
*
* File Name Functions
*
*******************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string FileNameExtension(string fullfilepath)
{
return new FileInfo(fullfilepath).Extension;
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string FileNameWithoutExtension(string fullfilepath)
{
var s = new FileInfo(fullfilepath).Name;
var ie = new FileInfo(fullfilepath).Extension;
var l = s.Length;
var el = ie.Length;
return s.Substring(0, l - (el));
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static bool IsLegalFileName(string filenamewithoutext)
{
// http://stackoverflow.com/questions/1976007/ddg#31976060
var invalidchars = new char[] { '/', '\\', '"', ':', '>', '<', '|', '?', '*' };
if (filenamewithoutext.Any(c => char.IsControl(c) || invalidchars.Contains(c)))
{
return false;
}
var invalidnames = new string[] {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
if (invalidnames.Contains(filenamewithoutext)) return false;
if (new char[] { '.', ' ' }.Contains(filenamewithoutext.Last())) return false;
return true;
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string FileNameWithExtension(string fullfilepath)
{
return new FileInfo(fullfilepath).Name;
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string FileInDirectory(string fullfilepath)
{
return new FileInfo(fullfilepath).DirectoryName;
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string FileInFolder(string fullfilepath)
{
return new FileInfo(fullfilepath).Directory.Parent.Name;
}
}
}