Skip to content

4.9 File Service Class

4.9.1 Uploading a Local File to the Robot

Method NameFileManager.Upload(string filePath , FileType ft , bool overWriting = false)
DescriptionUploads a local file to the robot controller.
Request ParametersfilePath : string Absolute path of the local file to be uploaded
ft : FileType Type of the file to be uploaded
overWriting : bool Whether to overwrite existing file in robot controller, default is false (no overwrite)
NoteFor USER_PROGRAM and BLOCK_PROGRAM files, only the program name needs to be specified without the file extension.
Return ValueStatusCode: Upload operation execution result
Compatible robot software versionCollaborative (Copper): v7.5.0.0+
Industrial (Bronze): v7.5.0.0+

4.9.2 Downloading a Robot File to a Local Machine

Method NameFileManager.Download(string fileName , FileType ft , string savePath )
DescriptionDownloads a file from the robot controller to local.
Request ParametersfileName : string Name of the file to be downloaded, no need to include extension
ft : FileType Type of the file to be downloaded
savePath : string Local save path for the downloaded file
Return ValueStatusCode: Download operation execution result
Compatible robot software versionCollaborative (Copper): v7.5.0.0+
Industrial (Bronze): v7.5.0.0+

4.9.3 Deleting a File from the Robot

Method NameFileManager.Delete(string fileName , FileType ft )
DescriptionDeletes a file from the robot controller.
Request ParametersfileName : string Name of the file to be deleted, no need to include extension
ft : FileType Type of the file to be deleted
Return ValueStatusCode: Delete operation execution result
Compatible robot software versionCollaborative (Copper): v7.5.0.0+
Industrial (Bronze): v7.5.0.0+

Example Code

FileManager/UserProgramOperations.cs
cs
using System.Collections.Generic;
using System.IO;
using Agilebot.IR;
using Agilebot.IR.FileManager;
using Agilebot.IR.Types;

public class UserProgramOperations
{
    /// <summary>
    /// 测试用户程序文件的完整操作流程:上传、下载、搜索和删除
    /// </summary>
    public static StatusCode Run(string controllerIP, bool useLocalProxy = true)
    {
        // [ZH] 初始化捷勃特机器人
        // [EN] Initialize the Agilebot robot
        Arm controller = new Arm(controllerIP, useLocalProxy);

        // [ZH] 连接捷勃特机器人
        // [EN] Connect to the Agilebot robot
        StatusCode code = controller.ConnectSync();
        Console.WriteLine(code != StatusCode.OK ? code.GetDescription() : "连接成功/Successfully connected.");

        if (code != StatusCode.OK)
        {
            return code;
        }

        try
        {
            Console.WriteLine("开始用户程序文件操作测试/Starting User Program File Operations Test");

            // [ZH] 获取测试文件路径
            // [EN] Get test file path
            string file_user_program = GetTestFilePath("test_prog.xml");
            string fileName = "test_prog";
            string save_path = GetTestFilePath("download");

            // [ZH] 上传用户程序文件
            // [EN] Upload user program file
            code = controller.FileManager.Upload(file_user_program, FileType.UserProgram, true);
            if (code == StatusCode.OK)
            {
                Console.WriteLine($"用户程序文件上传成功/User Program File Upload Success: {fileName}");
            }
            else
            {
                Console.WriteLine($"用户程序文件上传失败/User Program File Upload Failed: {code.GetDescription()}");
                return code;
            }

            // [ZH] 等待下载
            // [EN] Wait before download
            Thread.Sleep(1000);

            // [ZH] 下载用户程序文件
            // [EN] Download user program file
            code = controller.FileManager.Download(fileName, FileType.UserProgram, save_path);
            if (code == StatusCode.OK)
            {
                Console.WriteLine($"用户程序文件下载成功/User Program File Download Success: {fileName}");
            }
            else
            {
                Console.WriteLine($"用户程序文件下载失败/User Program File Download Failed: {code.GetDescription()}");
                return code;
            }

            // [ZH] 搜索用户程序文件
            // [EN] Search user program file
            List<string> results = new List<string>();
            (results, code) = controller.FileManager.Search(fileName);
            if (code == StatusCode.OK)
            {
                Console.WriteLine($"用户程序文件搜索成功/User Program File Search Success");
                Console.WriteLine($"搜索结果数量/Search Results Count: {results.Count}");
                foreach (var result in results)
                {
                    Console.WriteLine($"  找到文件/Found File: {result}");
                }
            }
            else
            {
                Console.WriteLine($"用户程序文件搜索失败/User Program File Search Failed: {code.GetDescription()}");
                return code;
            }

            // [ZH] 等待删除
            // [EN] Wait before delete
            Thread.Sleep(1000);

            // [ZH] 删除用户程序文件
            // [EN] Delete user program file
            code = controller.FileManager.Delete(fileName, FileType.UserProgram);
            if (code == StatusCode.OK)
            {
                Console.WriteLine($"用户程序文件删除成功/User Program File Delete Success: {fileName}");
            }
            else
            {
                Console.WriteLine($"用户程序文件删除失败/User Program File Delete Failed: {code.GetDescription()}");
                return code;
            }

            Console.WriteLine("用户程序文件操作测试完成/User Program File Operations Test Completed");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"执行过程中发生异常/Exception occurred during execution: {ex.Message}");
            code = StatusCode.OtherReason;
        }
        finally
        {
            // [ZH] 关闭连接
            // [EN] Close the connection
            StatusCode disconnectCode = controller.Disconnect();
            if (disconnectCode != StatusCode.OK)
            {
                Console.WriteLine(disconnectCode.GetDescription());
                if (code == StatusCode.OK)
                    code = disconnectCode;
            }
        }

        return code;
    }

    /// <summary>
    /// 获取test_files文件夹中文件的路径示例方法
    /// 展示如何获取当前程序目录下的test_files文件夹中的文件路径
    /// </summary>
    private static string GetTestFilePath(string fileName)
    {
        // [ZH] 获取当前程序集的目录
        // [EN] Get current assembly directory
        string? codeFilePath = new System.Diagnostics.StackTrace(true).GetFrame(0)?.GetFileName();
        if (string.IsNullOrEmpty(codeFilePath))
        {
            throw new InvalidOperationException("无法获取当前文件路径/Cannot get current file path");
        }

        string? codeDirectory = Path.GetDirectoryName(codeFilePath);
        if (string.IsNullOrEmpty(codeDirectory))
        {
            throw new InvalidOperationException("无法获取当前目录路径/Cannot get current directory path");
        }

        // [ZH] 构建test_files文件夹路径
        // [EN] Build test_files folder path
        string testFilesDirectory = Path.Combine(codeDirectory, "test_files");
        // [ZH] 构建文件完整路径
        // [EN] Build complete file path
        string filePath = Path.Combine(testFilesDirectory, fileName);
        return filePath;
    }
}

4.9.4 Searching for Files on the Robot Using a Pattern

Method NameFileManager.Search(string pattern , ref List<string> fl )
DescriptionSearches for files on the robot controller that match the pattern.
Request Parameterspattern : string File name matching pattern string
fl : ref List<string> Returned file list
Return ValueStatusCode: Search operation execution result
Compatible robot software versionCollaborative (Copper): v7.5.0.0+
Industrial (Bronze): v7.5.0.0+