【问题标题】:Can File.Delete cause a folder to open in Windows Explorer?File.Delete 可以导致文件夹在 Windows 资源管理器中打开吗?
【发布时间】:2019-11-13 08:24:26
【问题描述】:

我有一个非常奇怪的问题,在我的应用程序的某个位置,Windows 资源管理器似乎只是自己打开了一个文件夹。

如果我使用 Visual Studio 调试器单步执行,则不会发生这种情况,但通过策略性地设置断点,它似乎发生在 File.Delete 行的该区域。我通过在该行之前设置一个中断(它不会发生)并再次尝试在该行之后设置一个中断(它会)来得到这个

public static void RestoreIniFile()
{
    string strIniPath = RetrieveIniPath(); // This method figures just assigns a value to the string based on a registry setting. 

    if (File.Exists(strIniPath + "pdf995.ini_backup"))
    {
        if (File.Exists(strIniPath + "pdf995.ini"))
        {
            File.Delete(strIniPath + "pdf995.ini");
        }

        File.Move(strIniPath + "pdf995.ini_backup", strIniPath + "pdf995.ini");

    }

}

strIniPath 的值为C:\pdf995\res,但打开的目录为C:\pdf995\res\convert

当我删除上面的代码时,它会停止发生,但如果我把代码放回去,它就不会再次发生! (我必须卸载并重新安装应用程序才能让它再次开始)。上面的代码在执行时从两个不同的位置调用,大约相隔 1-2 秒,这发生在第二次运行时。

我也可以通过在代码中添加几行Thread.Sleep 来防止它,但是我真的不希望这是一个解决方案,因为它没有解决问题的根源而且效率也很低(延迟必须在半秒左右或更长时间)。

请注意,在两次调用之间会生成 PDF995 报告(这是导致现有延迟的原因),因此了解该应用程序的人可能会有所了解?也许删除ini文件会导致PDF995出于某种原因打开此文件夹?

或者,有没有一种方法可以获取有关导致应用程序在 Windows 本身或 Visual Studio 中启动的原因的信息?

编辑

这是RetrieveIniPath() 中的代码。它没有击中catch 部分。

public static string RetrieveIniPath()
{
    string strPdf995IniPath = "";

    try
    {
        string regPath = Consts.PDF995_PATH;

        RegistryKey regKey = null;
        regKey = Registry.LocalMachine.OpenSubKey(regPath);

        strPdf995IniPath = (String)regKey.GetValue("UninstallString", string.Empty);

        if (strPdf995IniPath.Contains("setup.exe"))
        {
            strPdf995IniPath = strPdf995IniPath.Substring(0, strPdf995IniPath.IndexOf("setup.exe")) + "res\\";
        }
        else
        {
            strPdf995IniPath = (String)regKey.GetValue("DisplayIcon", string.Empty);

            if (strPdf995IniPath.Length > 10)
            {
                strPdf995IniPath = strPdf995IniPath.Substring(0, strPdf995IniPath.Length - 9) + "res\\";
            }
            else
            {
                strPdf995IniPath = @"c:\pdf995\res\";
            }
        }
        regKey.Close();
    }
    catch (Exception ex)
    {
        LogManager.Instance().LogError(ex.Message, true);
    }

    if (strPdf995IniPath.Length == 0)
    {
        strPdf995IniPath = @"c:\pdf995\res\";
    }

    return strPdf995IniPath;
}

【问题讨论】:

  • 也许你有另一个线程改变了strIniPath。删除前可以在运行时添加strIniPath的日志吗?
  • @Ygalbel - strIniPath 是这个方法中的一个局部变量(我已经更新了问题来说明),无论如何我已经通过手表和 Console.WriteLines 检查了它的值和它确实有我假设的价值 (C:\pdf995\res)
  • File.Delete 没有打开任何东西。它与外壳和资源管理器没有任何关系。就是这样。您是否在代码中的任何地方使用Process.Start?如果您将文件夹路径作为可执行文件传递,资源管理器将打开该文件夹
  • 但是你说在调试中不会重现。当您不停止代码时,这将帮助您查看值。
  • @Ygalbel - 它确实发生在调试中,当我在调试中 step 时不会发生。此外,正如我在评论中所说,我使用了Console.WriteLine,它给出了相同的值。

标签: c# visual-studio explorer


【解决方案1】:

我的结论是 PDF995 模块本身(pdfsave 进程)打开文件夹以响应其 ini 文件被删除。如果文件被覆盖,它也会这样做,因此将Move 更改为Copy(带有覆盖)没有帮助。在我的应用程序执行到达File.Delete 时,该进程尚未完全关闭。

我的解决方案只是在替换文件之前等待进程退出。

while (true)
{
    Process[] pdfProcess = Process.GetProcessesByName("pdfsave");
    if (pdfProcess.Length == 0)
    {
        break;
    }

    Thread.Sleep(50);
}

if (File.Exists(strIniPath + "pdf995.ini"))
{
    File.Delete(strIniPath + "pdf995.ini");
}

File.Move(strIniPath + "pdf995.ini_backup", strIniPath + "pdf995.ini");

为什么删除,然后恢复代码导致它停止发生仍然是一个谜......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    相关资源
    最近更新 更多