File.Decrypt(String) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Decrypts a file that was encrypted by the current account using the Encrypt(String) method.
public:
static void Decrypt(System::String ^ path);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void Decrypt (string path);
public static void Decrypt (string path);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Decrypt : string -> unit
static member Decrypt : string -> unit
Public Shared Sub Decrypt (path As String)
Parameters
- path
- String
A path that describes a file to decrypt.
- Attributes
Exceptions
.NET Framework and .NET Core versions older than 2.1: The path
parameter is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.
The path
parameter is null
.
An invalid drive was specified.
The file described by the path
parameter could not be found.
An I/O error occurred while opening the file. For example, the encrypted file is already open.
-or-
This operation is not supported on the current platform.
The specified path, file name, or both exceed the system-defined maximum length.
The current operating system is not Windows NT or later.
The file system is not NTFS.
The path
parameter specified a file that is read-only.
-or-
This operation is not supported on the current platform.
-or-
The path
parameter specified a directory.
-or-
The caller does not have the required permission.
Examples
The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file. The file must exist for the example to work.
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "test.xml";
if (!File::Exists(fileName))
{
Console::WriteLine("The file " + fileName
+ " does not exist.");
return 0;
}
try
{
Console::WriteLine("Encrypt " + fileName);
// Encrypt the file.
File::Encrypt(fileName);
Console::WriteLine("Decrypt " + fileName);
// Decrypt the file.
File::Decrypt(fileName);
Console::WriteLine("Done");
}
catch (IOException^ ex)
{
Console::WriteLine("There was an IO problem.");
Console::WriteLine(ex->Message);
}
catch (PlatformNotSupportedException^)
{
Console::WriteLine("Encryption is not supported on " +
"this system.");
}
catch (NotSupportedException^)
{
Console::WriteLine("Encryption is not supported on " +
"this system.");
}
catch (UnauthorizedAccessException^)
{
Console::WriteLine("The operation could not be "
+ "carried out.");
}
}
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string FileName = "test.xml";
Console.WriteLine("Encrypt " + FileName);
// Encrypt the file.
AddEncryption(FileName);
Console.WriteLine("Decrypt " + FileName);
// Decrypt the file.
RemoveEncryption(FileName);
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
// Encrypt a file.
public static void AddEncryption(string FileName)
{
File.Encrypt(FileName);
}
// Decrypt a file.
public static void RemoveEncryption(string FileName)
{
File.Decrypt(FileName);
}
}
}
open System.IO
// Encrypt a file.
let addEncryption fileName = File.Encrypt fileName
// Decrypt a file.
let removeEncryption fileName = File.Decrypt fileName
let fileName = "test.xml"
printfn $"Encrypt {fileName}"
// Encrypt the file.
addEncryption fileName
printfn $"Decrypt {fileName}"
// Decrypt the file.
removeEncryption fileName
printfn "Done"
Imports System.IO
Imports System.Security.AccessControl
Module FileExample
Sub Main()
Try
Dim FileName As String = "test.xml"
Console.WriteLine("Encrypt " + FileName)
' Encrypt the file.
AddEncryption(FileName)
Console.WriteLine("Decrypt " + FileName)
' Decrypt the file.
RemoveEncryption(FileName)
Console.WriteLine("Done")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
' Encrypt a file.
Sub AddEncryption(ByVal FileName As String)
File.Encrypt(FileName)
End Sub
' Decrypt the file.
Sub RemoveEncryption(ByVal FileName As String)
File.Decrypt(FileName)
End Sub
End Module
Remarks
The Decrypt method allows you to decrypt a file that was encrypted using the Encrypt method. The Decrypt method can decrypt only files that were encrypted using the current user account.
Important
This API is only supported on Windows platforms that are able to use the NTFS Encrypting File System (EFS). Any attempt to use this on non-Windows systems, Windows Home Edition systems, or non-NTFS drives results in a PlatformNotSupportedException or NotSupportedException, depending on the situation.
Use of this API in .NET Core is not recommended; it is included to enable portability for applications that move to .NET Core but still explicitly target Windows.
The Decrypt method requires exclusive access to the file being decrypted, and will raise an exception if another process is using the file.
Both the Encrypt method and the Decrypt method use the cryptographic service provider (CSP) installed on the computer and the file encryption keys of the process calling the method.
The current file system must be formatted as NTFS and the current operating system must be Windows NT or later.
Applies to
Feedback
https://aka.ms/ContentUserFeedback.
Binnenkort: Gedurende 2024 worden GitHub Issues uitgefaseerd als het feedbackmechanisme voor inhoud. Dit wordt vervangen door een nieuw feedbacksysteem. Ga voor meer informatie naar:Feedback verzenden en bekijken voor