如何在列表框中显示此代码 [英] how to display this code in the listbox

查看:98
本文介绍了如何在列表框中显示此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在列表框中获取所有这些文件,但我不知道在哪里放这个代码



how to get all this files in the listbox but i dont know where put this code

private void CleanTemporaryFolders()
{
    String tempFolder = Environment.ExpandEnvironmentVariables("%TEMP%");
    String recent = Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "\\Recent";
    String prefetch = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\Prefetch";
    EmptyFolderContents(tempFolder);
    EmptyFolderContents(recent);
    EmptyFolderContents(prefetch);
}

private void EmptyFolderContents(string folderName)
{
    foreach (var folder in Directory.GetDirectories(folderName))
    {
        try
        {
            Directory.Delete(folder, true);
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
    foreach (var file in Directory.GetFiles(folderName))
    {
        try
        {
            File.Delete(file);
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
}

推荐答案

你不应该在你之前删除目录删除了其中的所有文件。您可能需要一个递归方法,它可以执行以下操作:

You should not be trying to delete the directories before you have deleted all the files in them. You probably need a recursive method that does something like:
Delete Method
Begin
    For Each Item in DirectoryPath
    Begin
        If Item is Directory
        Then
            Call Delete (DirectoryPath + Directory)
            Delete Directory
        ElseIf Item is File
        Then
            Delete File
        EndIf
    EndFor
End



如果你想要列表框中的项目,那么你需要在处理它们时添加它们。


If you want the items in a listbox then you need to add them as you process them.


您的问题并不完全清楚,但以下是如何收集您要删除的所有文件的名称。但是,请花点时间回顾一下这里的所有其他评论,你应该考虑很多有效的点。



Your question is not entirely clear, but here is how you can collect names of all the files you are deleting. However, please take the time to review all the other comments here, there is a lot of valid points you should really consider.

private List<string> EmptyFolderContents(string folderName)
{
    // directories omitted as you are only interested in files
    var deletedFiles = new List<string>();
    foreach (var file in Directory.GetFiles(folderName))
    {
        try
        {
            File.Delete(file);
            deletedFiles.Add(file); // file has been deleted successfully
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
    return deletedFiles;
}



您需要将列表提供给列表框。我认为该部分是明确的,因为您发布了与删除文件相关的代码,而不是与UI相关的代码。


The you need to feed the list into your list box. I assume that part is clear since you posted the code relevant to deleting the files, not the UI-related code.


这篇关于如何在列表框中显示此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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