1

我目前正在尝试删除文件夹中的所有文件(最近打开的文件),但没有任何运气,我收到消息:“访问路径 'C:\Users\User\Recent' 被拒绝。” . 我一直在四处寻找是否有任何解决方案,但不幸的是我找不到任何东西。

图片:

String recent = Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "\\Recent";
                        EmptyFolderContents(recent);

    private void EmptyFolderContents(string folderName)
        {

            foreach (var Folder in Directory.GetDirectories(folderName))
            {
                try
                {
                    Directory.Delete(Folder, true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            foreach (var Files in Directory.GetFiles(folderName))
            {
                try
                {
                   File.Delete(Files);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
        }
4

3 回答 3

1

您的错误消息似乎表明您缺少反斜杠:

拒绝访问路径“C:Users\User\Recent”。

(在 C 之后:)

于 2013-07-15T18:50:24.150 回答
1

The reason you are getting the Access Denied error is because you can't just delete files from that folder. They are virtual paths to real files. There is probably a better way to do what you are doing.

Here is an alternative that I found here. It uses WinAPI, which is never fun, but it works.

//you'll need to add this.
using System.Runtime.InteropServices;

[DllImport("shell32.dll")]
public static extern void SHAddToRecentDocs(ShellAddToRecentDocsFlags flag, IntPtr pidl);

public enum ShellAddToRecentDocsFlags
{
    Pidl = 0x001,
    Path = 0x002,
}

//then call this from your method
SHAddToRecentDocs(ShellAddToRecentDocsFlags.Pidl, IntPtr.Zero);
于 2013-07-15T19:06:09.333 回答
0

我使用 Permissions Time Machine v1.1 恢复默认权限并删除文件夹或文件或注册表项或 Windows 服务或 wmi 对象的“访问被拒绝”消息它是免费、快速和简单的

从 amyd 项目博客下载它

于 2013-10-11T08:23:22.913 回答