简体   繁体   中英

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

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?

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) - ie 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());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM