/
FileInfo.cs
208 lines (167 loc) · 7.46 KB
/
FileInfo.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Text;
using System.Runtime.Versioning;
namespace System.IO
{
// Class for creating FileStream objects, and some basic file management
// routines such as Delete, etc.
public sealed class FileInfo : FileSystemInfo
{
private FileInfo() { }
public FileInfo(string fileName)
: this(fileName ?? throw new ArgumentNullException(nameof(fileName)), isNormalized: false)
{
}
internal FileInfo(string originalPath, string? fullPath = null, string? fileName = null, bool isNormalized = false)
{
OriginalPath = originalPath;
fullPath ??= originalPath;
Debug.Assert(!isNormalized || !PathInternal.IsPartiallyQualified(fullPath.AsSpan()), "should be fully qualified if normalized");
FullPath = isNormalized ? fullPath ?? originalPath : Path.GetFullPath(fullPath);
_name = fileName;
}
public override string Name => _name ??= Path.GetFileName(OriginalPath);
public long Length
{
get
{
if ((Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, FullPath), FullPath);
}
return LengthCore;
}
}
public string? DirectoryName => Path.GetDirectoryName(FullPath);
public DirectoryInfo? Directory
{
get
{
string? dirName = DirectoryName;
if (dirName == null)
return null;
return new DirectoryInfo(dirName);
}
}
public bool IsReadOnly
{
get
{
return (Attributes & FileAttributes.ReadOnly) != 0;
}
set
{
if (value)
Attributes |= FileAttributes.ReadOnly;
else
Attributes &= ~FileAttributes.ReadOnly;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FileStream" /> class with the specified creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, additional file options and the allocation size.
/// </summary>
/// <remarks><see cref="FileStream(string,FileStreamOptions)"/> for information about exceptions.</remarks>
public FileStream Open(FileStreamOptions options) => File.Open(NormalizedPath, options);
public StreamReader OpenText()
=> new StreamReader(NormalizedPath, Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
public StreamWriter CreateText()
=> CreateStreamWriter(append: false);
public StreamWriter AppendText()
=> CreateStreamWriter(append: true);
public FileInfo CopyTo(string destFileName) => CopyTo(destFileName, overwrite: false);
public FileInfo CopyTo(string destFileName, bool overwrite)
{
ArgumentException.ThrowIfNullOrEmpty(destFileName);
string destinationPath = Path.GetFullPath(destFileName);
FileSystem.CopyFile(FullPath, destinationPath, overwrite);
return new FileInfo(destinationPath, isNormalized: true);
}
public FileStream Create()
{
FileStream fileStream = File.Create(NormalizedPath);
Invalidate();
return fileStream;
}
public override void Delete()
{
FileSystem.DeleteFile(FullPath);
Invalidate();
}
public override bool Exists
{
get
{
try
{
return ExistsCore;
}
catch
{
return false;
}
}
}
public FileStream Open(FileMode mode)
=> Open(mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None);
public FileStream Open(FileMode mode, FileAccess access)
=> Open(mode, access, FileShare.None);
public FileStream Open(FileMode mode, FileAccess access, FileShare share)
=> new FileStream(NormalizedPath, mode, access, share);
public FileStream OpenRead()
=> new FileStream(NormalizedPath, FileMode.Open, FileAccess.Read, FileShare.Read, File.DefaultBufferSize, false);
public FileStream OpenWrite()
=> new FileStream(NormalizedPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
// Moves a given file to a new location and potentially a new file name.
// This method does work across volumes.
public void MoveTo(string destFileName)
{
MoveTo(destFileName, false);
}
// Moves a given file to a new location and potentially a new file name.
// Optionally overwrites existing file.
// This method does work across volumes.
public void MoveTo(string destFileName, bool overwrite)
{
ArgumentException.ThrowIfNullOrEmpty(destFileName);
string fullDestFileName = Path.GetFullPath(destFileName);
// These checks are in place to ensure Unix error throwing happens the same way
// as it does on Windows.These checks can be removed if a solution to
// https://github.com/dotnet/runtime/issues/14885 is found that doesn't require
// validity checks before making an API call.
if (!new DirectoryInfo(Path.GetDirectoryName(FullName)!).Exists)
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullName));
if (!Exists)
throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, FullName), FullName);
FileSystem.MoveFile(FullPath, fullDestFileName, overwrite);
FullPath = fullDestFileName;
OriginalPath = destFileName;
_name = Path.GetFileName(fullDestFileName);
// Flush any cached information about the file.
Invalidate();
}
public FileInfo Replace(string destinationFileName, string? destinationBackupFileName)
=> Replace(destinationFileName, destinationBackupFileName, ignoreMetadataErrors: false);
public FileInfo Replace(string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors)
{
ArgumentNullException.ThrowIfNull(destinationFileName);
FileSystem.ReplaceFile(
FullPath,
Path.GetFullPath(destinationFileName),
destinationBackupFileName != null ? Path.GetFullPath(destinationBackupFileName) : null,
ignoreMetadataErrors);
return new FileInfo(destinationFileName);
}
[SupportedOSPlatform("windows")]
public void Decrypt() => File.Decrypt(FullPath);
[SupportedOSPlatform("windows")]
public void Encrypt() => File.Encrypt(FullPath);
private StreamWriter CreateStreamWriter(bool append)
{
StreamWriter streamWriter = new StreamWriter(NormalizedPath, append);
Invalidate();
return streamWriter;
}
}
}