启动进程无法删除文件
我有一个程序可以启动另一个程序。在第二个程序中,我尝试删除一个文件。 如果我直接运行第二个程序,没问题,文件会被删除。但是,如果我从第一个程序启动第二个程序,则会收到 System.UnauthorizedAccessException。
我的猜测是第二个程序没有获得第一个程序的所有访问权限。
我尝试了很多发现的建议,但没有一个有效。
添加 Process.StartInfo.Verb = "runas" 不起作用。
添加清单文件也不起作用(或者我做错了,不确定)
我将第二个文件的 .exe 设置为“以管理员身份运行”,但它不起作用。
现在,我该如何解决这个问题?
I have a program that start another program. In the second program, I try to delete a File.
If I run directly the second program, no problem, the file get deleted. But if I start the second program from the first program, I get a System.UnauthorizedAccessException.
My guess is that the second program doesn't get all the access of the first program.
I tried many suggestions I found but none of them worked.
Adding Process.StartInfo.Verb = "runas" didnt work.
Adding a manifest file didnt work either (or I did it wrong, not sure)
I set the .exe of the second file to "Run as Administrator" and it didn't work.
Now, how do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个程序使文件保持打开状态,因此第二个程序无法删除它。
您需要在第一个程序中关闭该文件。
The first program left the file open, so the second program cannot delete it.
You need to close the file in the first program.
您可能缺少对用于访问第一个程序中的文件的
FileStream
(例如)的Dispose()
调用。这可能会使底层文件在该程序中使用,尽管您认为与其关联的对象实例已经消失,因为它们超出了范围。如果您想要更好的反馈,请发布一些代码。
You might be missing a
Dispose()
call on theFileStream
(for example) used to access the file in the first program. That could leave the underlying file in use in that program, although you think the object instances associated with it are gone because they are out of scope.Post some code if you want better feedback.