删除安装程序中的临时文件夹有什么技巧?

发布于 2024-09-25 14:39:21 字数 199 浏览 3 评论 0原文

我正在通过 VS 2008 创建 MSI 安装程序。我尝试在安装结束时删除临时文件夹。该临时文件夹是由我的安装程序创建的,用于保存一些用于创建数据库的批处理文件。它总是显示其他进程正在访问它并且不允许我的代码删除它。我已调用该访问过程的 Close()。我已将 sleep 放在代码之前以删除它。没什么帮助。

您知道如何在安装结束时将其删除吗?

谢谢,

I am creating MSI installer via VS 2008. I try to delete the temp folder at the end of the installation. That temp folder is created by my installer to hold some batch files for database creation. It always show other process is access it and doesn't allow my code to delete it. I have called the Close() of that access process. I have put sleep before the code to delete it. Nothing helpful.

Do you have any idea how I can delete it at the end of the installation?

thanks,

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

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

发布评论

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

评论(3

咽泪装欢 2024-10-02 14:39:21

您是否尝试过 Filemon 来查看在对文件夹调用删除时谁正在访问临时文件夹?
最好使用系统临时文件夹路径,这样

System.Environment.GetEnvironmentVariable("TEMP")

您就不必担心清理它。

Did you try Filemon to see who is accessing the temp folder when delete is called on the folder?
Its better you use the system temp folder path

System.Environment.GetEnvironmentVariable("TEMP")

you need not worry about cleaning it up.

渡你暖光 2024-10-02 14:39:21

我可以考虑在临时文件夹中创建一个批处理文件来运行,该文件作为最后一步运行。您可以使用 ping (http://www.robvanderwoude.com/wait.php) 暂停它,然后几秒钟后(安装程序已退出)使用传递的参数删除该文件夹:

PING 1.1.1.1 -n 1 -w 60000 >NUL
rd "%1"

这确实是一个 hack。最好根除锁定文件夹的因素。

I can think of creating a batch file in the temp folder to run which runs as the last step. You put a pause in it by using ping (http://www.robvanderwoude.com/wait.php) and then after a few seconds (that installer has exited) delete the folder by using parameter passed:

PING 1.1.1.1 -n 1 -w 60000 >NUL
rd "%1"

This is really a hack. It is better to root out what locks your folder.

只是一片海 2024-10-02 14:39:21

我将首先解决您的设置中的概念问题:

  • 首先,正如“Vinay B R”所说,确保您的“temp”文件夹位于 Windows %TEMP% 文件夹下。这样,如果您无法删除这些文件,您可以将它们保留在那里。

  • 为什么完成后要删除批处理文件?您不需要自行清理 %TEMP% 文件夹内的内容。

  • 如果您想确保用户不会再次运行它们,那么您可以使用不同的文件扩展名命名它们(例如“.tmp”而不是.bat),使用此方法执行它们此处描述然后将它们抛在后面:

    <代码>cmd < "%TEMP%foo.tmp"

  • 如果您因为不希望用户访问它们而尝试删除文件,那么通过删除它们只能保护自己免受临时用户的侵害。< /p>

如果您仍然想删除这些文件,那么:

  • 很可能您自己的进程正在锁定您的文件夹。使用 Process Explorer 可能会指向 msiexec.exe 或 cmd.exe。毫无疑问,一旦 MSI 和 SQL 退出,您就可以手动删除该文件夹,对吧?如果是这样,那么您自己的进程不会立即终止。找出原因。 SQL 花费的时间是否比您想象的要长?

  • 作为阿利奥斯塔方法的替代方法,这里列出了“其他风格”本文。然而,正如他所写,您最好确定它被锁定的原因。

Process.Start("cmd.exe", "/C choice /C Y /N /D Y /T 3 & Del " +
    Application.ExecutablePath);
Application.Exit();

这是 C# 中的工作示例。如果您的用户安装了 .NET,那么您可以使用 WiX DTF 将其作为自定义操作调用(安装 WiX,然后在 Visual Studio 中选择“新建项目”->“Windows Installer XML”->“C# 自定义操作项目”):

// Note: This can also be used to delete this .exe (i.e.
// System.Windows.Forms.Application.ExecutablePath).
//
public static void AsynchDeleteFolder(string myTempFolderPath)
{
    ProcessStartInfo info = new ProcessStartInfo();

    // Don't create a visible DOS box.
    info.WindowStyle = ProcessWindowStyle.Hidden;
    info.CreateNoWindow = true;

    // Wait 3 seconds ("/T 3").
    info.Arguments = @"/C choice /C Y /N /D Y /T 3 & rmdir /S /Q """ +
        myTempFolderPath + @"""";

    info.FileName = "cmd.exe";
    Process.Start(info);
}

如果您只想将适用的部分作为批处理文件执行,那么您可以按照以下方法避免出现 DOS 窗口:

' Filename: Run_a_batch_file_with_no_popup_dos_box.vbs
'
' Invoke like this from the command line:
' wscript.exe Run_a_batch_file_with_no_popup_dos_box.vbs "c:\path with spaces to my file name.bat"
'
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & WScript.Arguments.Item(0) & Chr(34), 0
Set WshShell = Nothing

I'll address the conceptual problems in your setup first:

  • First off, as "Vinay B R" said, be sure your "temp" folder is beneath the Windows %TEMP% folder. That way, you can leave the files there if you fail to delete them.

  • Why exactly do you want to delete the batch files when you're done? There is no expectation that you clean up after yourself inside of the %TEMP% folder.

  • If you want to ensure the user doesn't run them again, then you could name them with a different file extension (e.g. ".tmp" instead of .bat), execute them using this method described here, then leave them behind:

    cmd < "%TEMP%foo.tmp"

  • If you are trying to delete files because you don't want the user to have access to them, then by deleting them you will only protect yourself against casual users.



If you still want to delete the files, then:

  • In all likelihood your own process is locking your folder. Using Process Explorer will likely point to msiexec.exe or cmd.exe. No doubt you are able to manually delete the folder once MSI and SQL have exited, right? If so, then your own process isn't terminating right away. Find out why. Is SQL taking longer than you think, perhaps?

  • As an alternative to Aliostad's method, here is the "other flavor" listed in this article. As he wrote, however, it would be best for you to determine why it's locked.

Process.Start("cmd.exe", "/C choice /C Y /N /D Y /T 3 & Del " +
    Application.ExecutablePath);
Application.Exit();



Here is a working sample in C#. If your users will have .NET installed, then you can invoke this as a custom action using the WiX DTF (install WiX, then in Visual Studio select New Project -> Windows Installer XML -> C# Custom Action Project).:

// Note: This can also be used to delete this .exe (i.e.
// System.Windows.Forms.Application.ExecutablePath).
//
public static void AsynchDeleteFolder(string myTempFolderPath)
{
    ProcessStartInfo info = new ProcessStartInfo();

    // Don't create a visible DOS box.
    info.WindowStyle = ProcessWindowStyle.Hidden;
    info.CreateNoWindow = true;

    // Wait 3 seconds ("/T 3").
    info.Arguments = @"/C choice /C Y /N /D Y /T 3 & rmdir /S /Q """ +
        myTempFolderPath + @"""";

    info.FileName = "cmd.exe";
    Process.Start(info);
}



If you would rather execute just the applicable portion as a batch file, then you can avoid the DOS window by following this method.:

' Filename: Run_a_batch_file_with_no_popup_dos_box.vbs
'
' Invoke like this from the command line:
' wscript.exe Run_a_batch_file_with_no_popup_dos_box.vbs "c:\path with spaces to my file name.bat"
'
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & WScript.Arguments.Item(0) & Chr(34), 0
Set WshShell = Nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文