access denied trying extracting an archive on the windows user temp folder
Asked Answered
A

2

0

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?

Amsden answered 3/11, 2008 at 10:19 Comment(0)
I
1

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());
Idiom answered 3/11, 2008 at 10:21 Comment(1)
thanks. turned out to be the long-path-name. enclosing the path in " solves it. another way to create temp directory name is this do { folderName = Path.GetRandomFileName(); path = Path.Combine(Path.GetTempPath(), folderName); } while (Directory.Exists(path));Amsden
A
0

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:

standard way to convert to short path in .net

Amsden answered 3/11, 2008 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.