views:

215

answers:

3

When I try to call System.IO.File.Encrypt() on an existing file, it throws a generic IOException, and the message is "Parameter is incorrect".

System.IO.File.Encrypt("C:\Project\StorageDirectory\file.txt")

The current user the process is running under has full control to the "StorageDirectory" folder. Is there something I'm missing permissions-wise?

A: 

Well reading the MSDN page,

An I/O error occurred while opening the file.

-or-

This operation is not supported on the current platform.

Neither is a particularly useful statement, but they should give you somewhere to start.

  • Can you read the file? Have you tested it from within your app?

  • Have you done encryption elsewhere? I've never seen encryption fail like that before (but there's always a first time!)

Oli
I will give your suggestion a shot.
Chris Dwyer
Just tried it... I'm able to read the data from the file just before I try to encrypt the file.
Chris Dwyer
What about writing?
Oli
Yep, writing works too.
Chris Dwyer
+1  A: 

The docs say this could be caused by

An I/O error occurred while opening the file. -or- This operation is not supported on the current platform.

It could be that the file is locked by another process, or that you don't have permission to modify it. You could run ProcMon while you reproduce this error to see if that's your problem.

Kevin Tighe
I'm guessing you're right about the file being locked, but by what, I don't know. I'll try your suggestion.
Chris Dwyer
I used ProcMon, and from what I saw, it suggested that the user needs certain rights to the entire path. I added the user to the local admin group, but still got the same error.
Chris Dwyer
A: 

You need to escape the backslash characters (notice the @ character)

System.IO.File.Encrypt(@"C:\Project\StorageDirectory\file.txt")
OutOfMemory
Well now I see that you have a vb.net tag instead of c#, so forget it ;)
OutOfMemory