尝试在 Windows 用户临时文件夹中提取存档时访问被拒绝

发布于 2024-07-08 19:08:52 字数 322 浏览 16 评论 0 原文

我正在尝试对位于 Windows 用户临时目录上的临时文件夹中的文件运行命令行进程(即提取 .7z 存档) (C:\Documents and Settings\User\Local Settings\Temp),在我的 c# 应用程序中使用 Process。

我认为进程返回错误是由于“访问被拒绝”而发生的,因为当我深入 .NET 的 procoess 对象时,我可以看到错误代码为 5 的 win32Exception 。

以前在其他地方做同样的事情效果很好,所以我想也许这是我不应该做的事情? (运行进程以使用 %TEMP% 上的文件) 也许我需要以某种方式通过安检?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-07-15 19:08:52

假设您使用的是常规 .NET(不是 CF/Silverlight 等),则完全可以访问用户临时区域中的文件。 我想知道问题是否不在于您在创建文件后不小心将文件保持打开状态,也许是因为没有使用“使用”或类似的内容?

我可能不建议在使用单独的进程时使用环境变量(%TEMP% 等); 理想情况下,您将传递文件的完整路径(更少出错...),确保引用任何路径参数(如果有空格) - 即您的参数是@“ ... ""c:\some path\whatever\tmp""..." (如果你明白我的意思)。

最后,如果您要提取文件,则需要考虑现有内容。 Path.GetTempFileName() 非常适合创建单个文件占位符,但是为了提取存档,您可能需要创建一个目录 - guid 对于此目的很方便(同时避免冲突,并记住之后将其删除):

string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

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());
温柔嚣张 2024-07-15 19:08:52

使用命令行(cmd)运行相同的进程有助于找出我的问题是我使用长路径名指定了进程的路径参数。

可以在这里找到解决方案:

转换为短路径的标准方法.net 中的路径

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文