Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to test the File.Encrypt and File.Decrypt methods but I am getting errors. Had a look at the MSDN site but no solution to my problem have I found. Am I missing something?

C#
private void button2_Click(object sender, EventArgs e)
{
    File.Decrypt("Text.pvf");
    string[] DataFile = File.ReadAllLines("Text.pvf");
    if (DataFile[5] == "6")
        MessageBox.Show("Encrypt/Decrypt successful");
}

private void button1_Click(object sender, EventArgs e)
{
    string[] DataFile = new string[6];
    DataFile[0] = "1";
    DataFile[1] = "2";
    DataFile[2] = "3";
    DataFile[3] = "4";
    DataFile[4] = "5";
    DataFile[5] = "6";
    File.WriteAllLines("Text.pvf", DataFile);
    File.Encrypt("Text.pvf");
}


I am getting an error message when button1 is clicked, "File.Encrypt("Text.pvf");" returns an error exception, "The request is not supported."

Maybe copy my code to test it. Maybe that will help in determining the error.
Please help with this as I don't what the cause is
Posted
Updated 3-Feb-12 0:48am
v3
Comments
Richard MacCutchan 3-Feb-12 5:54am    
Which version of windows are you running on?
Winston_D 3-Feb-12 6:35am    
I am running Windows7 64bit, .Net 4.0, NTFS file system. Any pointers on the cause of the error?
Richard MacCutchan 3-Feb-12 9:39am    
I have just tried this on my Windows 7 system and receive exactly the same error. despite checking with my debugger I can find no explanation. I suggest you try the Microsoft forums to see if they have any ideas why it happens.

I suppose you're talking about NotSupportedException.
You may look up its meaning in your context cheking out the method's documentation[^]: "The file system is not NTFS".
 
Share this answer
 
Comments
Winston_D 3-Feb-12 5:51am    
"The request is not supported" is the exception I get at the line: "File.Encrypt("Text.pvf");"
CPallini 3-Feb-12 6:00am    
Is the file system NTFS?
Winston_D 3-Feb-12 6:30am    
File system is NTFS yes
Winston_D 3-Feb-12 6:31am    
Windows 7 64bit and .Net 4.0 is my pc config
Winston_D 3-Feb-12 6:33am    
Can you run the supplied code and possibly point out the cause of this error?
Tested this with Win7, 64-bit, NTFS and it worked perfectly. Could it be that you don't have the permission to write to the file (file permission, directory permission, file is read only...). This would also cause the exception mentioned.
 
Share this answer
 
Comments
Winston_D 4-Feb-12 2:46am    
I checked the file properties and the advanced settings. the checkbox for the 'Encrypt file to preserve contents' is unavaliable. I take it that it is the problem. How do I solve this?
Wendelius 4-Feb-12 7:20am    
See the updated comments about Home Edition.
Richard MacCutchan 4-Feb-12 4:13am    
I tried this with a file that I created in code (same as OP) and then an existing file that I could edit, and same problem with both. This is nothing to do with the file, it is the system reporting that it does not support this feature.
Wendelius 4-Feb-12 6:40am    
Yes, I agree, but I found some problems giving the exact same error when the file was read-only.

Another thing that could possibly cause this is if the Windows is 7 Home edition. AFAIK EFS isn't included in that edition.
Richard MacCutchan 4-Feb-12 7:17am    
You're right, see my latest Solution entry.
Files marked with the System attribute and files in the systemroot directory cannot b encrypted.
chk if this is the problem.


change file path and try .
 
Share this answer
 
Comments
Richard MacCutchan 4-Feb-12 4:11am    
It's definitely not the problem on my Windows 7 system.
Winston_D 6-Feb-12 2:33am    
Neither is that my problem. I am running Windows7 Home Premium 64bit. a pity it is Home P cuz that looks to be the problem. Thanx for the help, oh, my file is saved in a valid path that 'should' allow encrypt/decrypt without problems. Looks like the OS is the problem
See these comments[^] by Microsoft, maybe it applies to your system. I am on Windows 7 Home Premium and encryption looks like it is definitely not supported by default.
 
Share this answer
 
Comments
Winston_D 6-Feb-12 2:36am    
Thanx for the help. We have the same OS problems it seems

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900