DSInternals/Src/DSInternals.Common/Extensions/StringExtensions.cs

47 lines
1.5 KiB
C#
Raw Normal View History

namespace DSInternals.Common
2015-12-26 22:44:43 +00:00
{
using DSInternals.Common.Interop;
using System.Security;
using System.Security.AccessControl;
2015-12-26 22:44:43 +00:00
public static class StringExtensions
{
public static string TrimEnd(this string input, string suffix)
{
if(! string.IsNullOrEmpty(input) && ! string.IsNullOrEmpty(suffix) && input.EndsWith(suffix))
{
int trimmedLength = input.Length-suffix.Length;
return input.Remove(trimmedLength);
}
else
{
return input;
}
}
2015-12-26 22:44:43 +00:00
public static SecureString ToSecureString(this string input)
{
if (input == null)
{
return null;
}
SecureString output = new SecureString();
foreach (char c in input.ToCharArray())
{
output.AppendChar(c);
}
output.MakeReadOnly();
return output;
}
2016-02-28 14:21:22 +00:00
public static byte[] SddlToBinary(this string securityDescriptor)
{
Validator.AssertNotNullOrWhiteSpace(securityDescriptor, "securityDescriptor");
byte[] binarySecurityDescriptor;
Win32ErrorCode result = NativeMethods.ConvertStringSecurityDescriptorToSecurityDescriptor(securityDescriptor, GenericSecurityDescriptor.Revision, out binarySecurityDescriptor);
Validator.AssertSuccess(result);
return binarySecurityDescriptor;
}
2015-12-26 22:44:43 +00:00
}
}