编辑:应该在哪里以及如何下载临时文件?
假设我想从网络上运行一个 exe,显然我宁愿将其下载到临时文件夹中..从那里运行它,然后删除它(或让操作系统这样做)。是否有任何约定如何做到这一点,同时确保不会陷入权限问题、覆盖现有文件等?
谢谢。
编辑 我通过桌面应用程序进行操作。 顺便说一句,我确实有很多猜测,但我只是想知道什么被接受了。
我知道我可以使用 Path.GetTempFile ,但它已经为我创建了文件,这让我认为它也可以将其添加到某些数据库中,并跟踪它 - 所以我可以'不要删除它并用我自己的文件替换它。
我知道我也可以使用 Path.TempDirectory (或类似的东西)和 Path.RandomFileName ,但后者为文件名添加随机扩展名,而我需要 exe,显然这也可以很容易地解决,但对我来说重写 MS 已经尝试为我们编写的内容似乎很奇怪。
Say I want to run an exe from the web, well obviously I rather download it to a temp folder.. run it from there, and delete it (or let the OS do so). Is there any convention how to do that, while being sure not to bomb into permissions issues, overriding exist file etc.?
Thanks.
EDIT I ment from a desktop application.
BTW, I do have a lot of guess, but I'm just wondering what's accepted.
I know i can use Path.GetTempFile
, but then it already made the file for me, which makes me thinking that it may also add it to some db, and keep track on it - so i can't delete it and replace it by my own file.
I know i can also use Path.TempDirectory
(or something similar) and Path.RandomFileName
, but the latter add a random extension to the file name, while i need exe, obviously this can be solven very easily as well, but it seems weird to me to rewrite what seems that MS already tried to wrote for us.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我需要编写一个自动更新的 Windows 服务,并发现自己处于类似的境地,我需要在执行之前将最新的安装程序下载到临时位置。关于该领域最佳实践的信息并不多,因此我依赖于 ASP.NET/C# 提供的“工具”。
我使用 Path.GetTempPath() 和下载的文件名构建了下载文件的完整路径。下面是一些示例代码:
最重要的是,我认为您猜测使用 System.IO.Path Temp* 函数是一种合理的方法 - 尽管我找不到任何文档来支持它。
I had the need to write a self-updating Windows Service and found myself in similar shoes where I needed to download the latest installer to a temporary location prior to executing. There isn't much information regarding best practices in this area so I relied upon the "tools" which ASP.NET/C# offered.
I built the full path for the downloaded file using Path.GetTempPath() and the downloaded file name. Here's some sample code:
The bottom line is I think your guess to use the System.IO.Path Temp* functions is a reasonable approach -- even though I can't find any documentation to support it.
受信任的小程序可以使用用户的文件系统,因此您可以走这条路。您正在寻求 C# 的建议,但如果该语言灵活的话,这也适用于 Java 世界。
Trusted applets can play with the user's file system, so you could go that route. You're asking for C# advice, but this would work in the Java world if the language is flexible.
您可以查看隔离存储。
You could look into Isolated Storage.
您是否查看过ClickOnce 部署?
Have you looked at ClickOnce Deployment ?