Unity中使用批处理删除文件或文件夹、清空回收站

该文章展示了如何在Unity中编写C#代码来运行.bat批处理文件,实现删除指定路径的文本文件和文件夹。代码中包含了对回收站的静默清理,并处理了可能出现的异常情况。

先写.bat文件

@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

再写一段unity代码:

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.StartI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值