当用户在运行时将其文件弄乱时,如何防止桌面应用程序严重损坏? [英] How can I prevent my desktop application from breaking horribly when the user messes with its files at run-time?

查看:74
本文介绍了当用户在运行时将其文件弄乱时,如何防止桌面应用程序严重损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在运行期间删除输出文件,将sw的两个实例定向到相同的IO等吗?

Such as deleting the output file during run, directing two instances of the sw to the same IO etc ?

推荐答案

您可以采取很多措施来阻止用户这样做,但是您可以应用

There isn't much you can do to prevent the user from doing that, but what you can do is to apply defensive programming.

在两个示例中,您每次访问资源时都必须检测故障情况,并做出适当的反应,要么抛出异常并死亡,要么继续访问该资源.

In your two examples you'd have to, every time you access a resource, detect failure conditions and react appropriately either throwing exceptions and dying or continuing without access to that resource.

永远不要假设资源分配会成功,请始终检查您无法控制的内容.简而言之,假设尽可能少,并验证所有内容.

Never assume a resource allocation will succeed, always check what you don't control. In short, assume as little as possible and verify everything.

'被告知要访问此文件以对其进行写入.它已经开放了吗?我有权限吗?它具有预期的格式吗?磁盘是否有足够的空间?等等.

还有更多有关此的内容.只是Google进行防御性编程,您将学到很多东西.

There is many more about this. Just Google for defensive programming and you'll get a lot to read about.

这篇关于当用户在运行时将其文件弄乱时,如何防止桌面应用程序严重损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆