views:

720

answers:

2

I'm trying to run a command-line process (which is extraction of a .7z archive) on a file that lies in a temporary folder on the windows user temp directory (C:\Documents and Settings\User\Local Settings\Temp), using Process in my c# app.

I think the process return error that happens because of "access denied" because I can see a win32Exception with error code 5 when I dig in the prcoess object of .NET.

doing the same on some other location worked fine before, so I guess maybe it's something I'm not supposed to do ? (running a process to use a file on the the %TEMP%) perhaps I need to pass security somehow?

+1  A: 

Assuming that you are using regular .NET (not CF/Silverlight, etc) Accessing files in the user's temp area is entirely expected. I wonder if the problem isn't more that you've accidentally left the file open after creating it, perhaps by not using a "using" or similar?

I probably wouldn't suggest using environment variables (%TEMP% etc) when shelling out to a separate process; ideally you'd pass the full path to the file (less things to get wrong...), making sure to quote any path arguments (in case of space) - i.e. so your args are @"... ""c:\some path\whatever\tmp""..." (if you see what I mean).

Finally, if you are extracting files, you need to think about the existing contents. Path.GetTempFileName() is fine for creating a single file place-holder, but for extracting an archive you probably want to create a directory - guids are handy for this purpoes (while avioding conflicts, and remember to remove it afterwards):

string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Marc Gravell
thanks. turned out to be the long-path-name. enclosing the path in " solves it.another way to create temp directory name is thisdo{ folderName = Path.GetRandomFileName(); path = Path.Combine(Path.GetTempPath(), folderName);}while (Directory.Exists(path));
Hanan
A: 

running the same process using command-line (cmd) helped to figure out my problem was that I specified path arguments to the process using long-path-name.

Solution to this can be found here:

http://stackoverflow.com/questions/258367/standard-way-to-convert-to-long-path-in-net

Hanan