Makes the test pass when EFS (Encrypted File System) is not available.
I have also fixed the interop definition for Encrypt/DecryptFile P/Invokes while I was on it.
Fixes dotnet/corefx#39211
Commit migrated from https://github.com/dotnet/corefx/commit/
8c8bf07d52389d8a2948460d4f7e0dba112c20a0
internal partial class Interop
{
- internal partial class Kernel32
+ internal partial class Advapi32
{
/// <summary>
/// WARNING: This method does not implicitly handle long paths. Use EncryptFile.
/// </summary>
- [DllImport(Libraries.Advapi32, EntryPoint = "EncryptFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
+ [DllImport(Libraries.Advapi32, EntryPoint = "EncryptFileW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool EncryptFilePrivate(string lpFileName);
internal static bool EncryptFile(string path)
/// <summary>
/// WARNING: This method does not implicitly handle long paths. Use DecryptFile.
/// </summary>
- [DllImport(Libraries.Advapi32, EntryPoint = "DecryptFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
+ [DllImport(Libraries.Advapi32, EntryPoint = "DecryptFileW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool DecryptFileFilePrivate(string lpFileName, int dwReserved);
internal static bool DecryptFile(string path)
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.GenericOperations.cs">
<Link>Common\Interop\Windows\Interop.GenericOperations.cs</Link>
</Compile>
- <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.EncryptDecrypt.cs">
+ <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs">
<Link>Common\Interop\Windows\Interop.EncryptDecrypt.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.GetVolumeInformation.cs">
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.CreateFile.cs">
<Link>Common\Interop\Windows\Interop.CreateFile.cs</Link>
</Compile>
- <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.EncryptDecrypt.cs">
+ <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs">
<Link>Common\Interop\Windows\Interop.EncryptDecrypt.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
{
string fullPath = Path.GetFullPath(path);
- if (!Interop.Kernel32.EncryptFile(fullPath))
+ if (!Interop.Advapi32.EncryptFile(fullPath))
{
ThrowExceptionEncryptDecryptFail(fullPath);
}
{
string fullPath = Path.GetFullPath(path);
- if (!Interop.Kernel32.DecryptFile(fullPath))
+ if (!Interop.Advapi32.DecryptFile(fullPath))
{
ThrowExceptionEncryptDecryptFail(fullPath);
}
string fileContentRead = File.ReadAllText(tmpFileName);
Assert.Equal(textContentToEncrypt, fileContentRead);
- File.Encrypt(tmpFileName);
+ try
+ {
+ File.Encrypt(tmpFileName);
+ }
+ catch (IOException e) when (e.HResult == unchecked((int)0x80070490))
+ {
+ // Ignore ERROR_NOT_FOUND 1168 (0x490). It is reported when EFS is disabled by domain policy.
+ return;
+ }
+
Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName));
Assert.Equal(FileAttributes.Encrypted, (FileAttributes.Encrypted & File.GetAttributes(tmpFileName)));