File::Encrypt Method (String^)
Encrypts a file so that only the account used to encrypt the file can decrypt it.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
-
Type:
System::String^
A path that describes a file to encrypt.
| Exception | Condition |
|---|---|
| ArgumentException | The path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars. |
| ArgumentNullException | The path parameter is null. |
| DriveNotFoundException | An invalid drive was specified. |
| FileNotFoundException | The file described by the path parameter could not be found. |
| IOException | An I/O error occurred while opening the file. -or- This operation is not supported on the current platform. |
| PathTooLongException | The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. |
| PlatformNotSupportedException | The current operating system is not Windows NT or later. |
| NotSupportedException | The file system is not NTFS. |
| UnauthorizedAccessException | 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. |
The Encrypt method allows you to encrypt a file so that only the account used to call this method can decrypt it. Use the Decrypt method to decrypt a file encrypted by the Encrypt method.
The Encrypt method requires exclusive access to the file being encrypted, and will fail 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.
This method is not available on all versions of Windows. For example, it is not available on Home editions.
The current file system must be formatted as NTFS.
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."); } }
for permission to read and write to the file described by the path parameter. Security action: Demand. Associated enumerations: FileIOPermissionAccess::Read, FileIOPermissionAccess::Write
Available since 2.0