简体   繁体   English

如何到达 appdata 中的文件夹并将其删除?

[英]How do I reach a folder in the appdata and delete it?

I'm trying to create a launcher for a FiveM server, currently I'm at the "delete cache" button, the principle is that when you press it, it deletes the folders: "browser,db,dunno,priv,servers,subprocess" they are located at: \\AppData\\Local\\FiveM\\FiveM.app\\cache' but I don't know how to reach them deletes, here's the code I tried and didn't work, they do not delete themselves (I'm new in C#)我正在尝试为 FiveM 服务器创建一个启动器,目前我在“删除缓存”按钮上,原理是当你按下它时,它会删除文件夹:“浏览器、数据库、dunno、priv、服务器、 subprocess”,它们位于:\\AppData\\Local\\FiveM\\FiveM.app\\cache' 但我不知道如何到达它们删除,这是我尝试过但不起作用的代码,它们不会删除自己(我我是 C# 的新手)

Here's the code:这是代码:

        private void button3_Click(object sender, EventArgs e)
        {
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string path = System.IO.Path.Combine(folder, "fivem");
            string fap = System.IO.Path.Combine(path, "FiveM Application Data");
            string cache = System.IO.Path.Combine(fap, "cache");
            string browser = System.IO.Path.Combine(cache, "browser");
            string db = System.IO.Path.Combine(browser, "db");
            string dunno = System.IO.Path.Combine(db, "dunno");
            string priv = System.IO.Path.Combine(dunno, "priv");
            string servers = System.IO.Path.Combine(priv, "servers");
            string Cache = System.IO.Path.Combine(servers, "subprocess");



            if (Directory.Exists(Cache))
            {
                Directory.Delete(Cache);
            }
        }

I'm not sure that Path.Combine works in that way.我不确定Path.Combine以这种方式工作。 I would modify your method like this (easier to read and maintain):我会像这样修改你的方法(更容易阅读和维护):

    private void button3_Click(object sender, EventArgs e)
    {
        string baseFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"fivem", "FiveM.app");

        var folders = new string[]
        {
            "FiveM Application Data",
            "cache",
            "browser",
            "db",
            "dunno",
            "priv",
            "servers",
            "subprocess"
        };

        foreach (var folder in folders)
        {
            var toDelete = System.IO.Path.Combine(baseFolder, folder);

            if (Directory.Exists(toDelete))
            {
                Directory.Delete(toDelete, true);
            }
        }
    }

if you want to delete ALL the folders inside your base folder, without choosing them by name, you can try this:如果您想删除基本文件夹中的所有文件夹,而不按名称选择它们,您可以尝试以下操作:

    private void button3_Click(object sender, EventArgs e)
    {
        string baseFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"fivem", "FiveM.app");

        foreach (var subDir in new DirectoryInfo(baseFolder).GetDirectories()) {
            subDir.Delete(true);
        }
    }

Don't forget to use the Delete overload with true parameter, so you can launch a recursive delete to remove all files and subfolders不要忘记使用带有true参数的Delete重载,这样您就可以启动递归删除以删除所有文件和子文件夹

Try:尝试:

private void button3_Click(object sender, EventArgs e)
        {
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string path = System.IO.Path.Combine(folder, "fivem");
            string fap = System.IO.Path.Combine(path, "FiveM Application Data");
            string cache = System.IO.Path.Combine(path, "cache");
            string browser = System.IO.Path.Combine(path, "browser");
            string db = System.IO.Path.Combine(path, "db");
            string dunno = System.IO.Path.Combine(path, "dunno");
            string priv = System.IO.Path.Combine(path, "priv");
            string servers = System.IO.Path.Combine(path, "servers");
            string subprocess = System.IO.Path.Combine(path, "subprocess");

        if (Directory.Exists(fap))
        {
            Directory.Delete(fap, true);
        }
        if (Directory.Exists(cache))
        {
            Directory.Delete(cache, true);
        }
        //repeat for all the variables

        if (Directory.Exists(subprocess))
        {
            Directory.Delete(subprocess, true);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM