删除临时目录

发布于 2024-07-12 13:58:14 字数 667 浏览 2 评论 0原文

我有这段代码,

showmessage('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID);
if removedir('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID) then
  showmessage('Removed')
else
  showmessage('Failed');

消息显示 C:\TEMP\0 并且该目录确实存在,因为程序之前创建了它并使用了其中的文件,然后删除了它们。 我可以看到文件和目录,所以我知道它们在那里。 程序成功删除文件,但未删除目录。

如果我对它的目录进行硬编码 - 这意味着它接受字符串 C:\TEMP\0 但不接受 C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID 两者都等于 C:\TEMP\ 0 。 我无法对这些目录进行硬编码,那么我该怎么办? 如何从 string + string 转换为 removedir() 所期望的内容。 我查了一下 Delphi 基础知识,它需要一个字符串。

我很困惑,因为字符串+字符串=字符串。 到底是怎么回事?

I have this code,

showmessage('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID);
if removedir('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID) then
  showmessage('Removed')
else
  showmessage('Failed');

The message shows C:\TEMP\0 and this directory does exist as the program created it earlier and used files inside it and then later deletes them. I can see the files and directories so I know they're there. The program successfully deletes the files but does not remove the directory.

If I hardcode the directory it works - this means that it accepts the string
C:\TEMP\0 but does not accept C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID both equate to C:\TEMP\0. I cannot hardcode these directories, so what can I do? How do I convert from a string + string to whatever removedir() is expecting. I looked this up at Delphi basics and it's expecting a string.

I'm confused, since string + string = string. What is going on?

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

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

发布评论

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

评论(4

灰色世界里的红玫瑰 2024-07-19 13:58:14

确保您的程序或任何其他程序都没有将该目录作为其当前工作目录。 当您重新编译程序时,情况可能不再如此,因此硬编码值对您有用可能会转移注意力。

Make sure that neither your program nor any other program have the directory as their current working directory. When you recompile the program this may no longer be the case, so it may be a red herring that the hardcoded value works for you.

粉红×色少女 2024-07-19 13:58:14

除了其他好的答案之外,您不应将临时文件夹存储在 C:\TEMP 中。 请改用 GetTempFilename 返回的值。 与 C:\TEMP 不同,此位置(因操作系统而异)适用于所有操作系统以及所有级别的用户访问控制。 这还消除了您硬编码的位置也可能被硬编码到另一个系统中的风险。

In addition to the other good answers, you should not be storing your temp folder in C:\TEMP. Use the value returned from GetTempFilename, instead. Unlike C:\TEMP, this location (which varies by operating system) will work on all operating systems, and all levels of user access control. This also eliminates the risk that the location you have hardcoded might also be hardcoded into another system.

白昼 2024-07-19 13:58:14

如果我理解正确的话,openfiles[openfilelist.ItemIndex].ID 是一个包含数字的字符串?
如果是这样,您是否检查过它不包含空格? 像这样的事情:

filename := 'C:\TEMP\' + trim(openfiles[openfilelist.ItemIndex].ID);
showmessage(filename);
if removedir(filename) then
   showmessage('Removed')
else
   showmessage('Failed');

If I understood correctly, openfiles[openfilelist.ItemIndex].ID is a string that contains number?
If so, did you check that it does not contain blanks? Something like this:

filename := 'C:\TEMP\' + trim(openfiles[openfilelist.ItemIndex].ID);
showmessage(filename);
if removedir(filename) then
   showmessage('Removed')
else
   showmessage('Failed');
無心 2024-07-19 13:58:14

openfiles 和 openfilelist 是什么类型的对象?

他们是否打开文件夹,如果是的话,当您尝试删除文件夹时它们可能仍然打开。

What type of objects are openfiles and openfilelist?

Do they open folders at all, if so they may still be open when your trying to delete the folder.

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