Use batch processing in Unity to delete files or folders and empty the Recycle Bin

Write the .bat file first

@REM 删除指定路径文件
del /f C:\Users\admin\Desktop\abc.txt
@REM 删除指定路径文件夹
rd /s /q C:\Users\admin\Desktop\qwe

@REM 静默清空回收站  询问去掉 /q
rd /s /q c:\$Recycle.Bin

Write another piece of unity code:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class SampleScene : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var path = FormatPath(Application.streamingAssetsPath + "/del.bat");
        Debug.LogError(path);

        if (!File.Exists(path))
        {
            Debug.LogError("bat文件不存在");
        }
        else
        {
            System.Diagnostics.Process proc = null;
            try
            {
                proc = new System.Diagnostics.Process();
                proc.StartInfo.WorkingDirectory = Application.streamingAssetsPath + "/";
                proc.StartInfo.FileName = "del.bat";
                //proc.StartInfo.Arguments = args;
                //proc.StartInfo.CreateNoWindow = true;
                //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//disable dos window
                proc.Start();
                proc.WaitForExit();
                proc.Close();
            }
            catch (System.Exception ex)
            {
                Debug.LogFormat("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
            }
        }
    }
    
    string FormatPath(string path)
    {
        path = path.Replace("/", "\\");
        if (Application.platform == RuntimePlatform.OSXEditor)
            path = path.Replace("\\", "/");
        return path;
    }
}

Guess you like

Origin blog.csdn.net/ThreePointsHeat/article/details/130891791