This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGetFileEncodingCommand.cs
More file actions
70 lines (57 loc) · 2.03 KB
/
GetFileEncodingCommand.cs
File metadata and controls
70 lines (57 loc) · 2.03 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO;
using System.Management.Automation;
using System.Text;
namespace Microsoft.PowerShell.TextUtility
{
/// <summary>
/// This class implements the Get-FileEncoding command.
/// </summary>
[Cmdlet(VerbsCommon.Get, "FileEncoding", DefaultParameterSetName = PathParameterSet)]
[OutputType(typeof(Encoding))]
public sealed class GetFileEncodingCommand : PSCmdlet
{
#region Parameter Sets
private const string PathParameterSet = "ByPath";
private const string LiteralPathParameterSet = "ByLiteralPath";
#endregion
#region Parameters
/// <summary>
/// Gets or sets path from from which to get encoding.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = PathParameterSet)]
public string Path { get; set; }
/// <summary>
/// Gets or sets literal path from which to get encoding.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = LiteralPathParameterSet)]
[Alias("PSPath", "LP")]
public string LiteralPath
{
get
{
return _isLiteralPath ? Path : null;
}
set
{
Path = value;
_isLiteralPath = true;
}
}
private bool _isLiteralPath;
#endregion
/// <summary>
/// Process paths to get file encoding.
/// </summary>
protected override void ProcessRecord()
{
string resolvedPath = PathUtils.ResolveFilePath(Path, this, _isLiteralPath);
if (!File.Exists(resolvedPath))
{
PathUtils.ReportPathNotFound(Path, this);
}
WriteObject(PathUtils.GetPathEncoding(resolvedPath));
}
}
}