You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2022使用COSMOS构建时System.Exception异常解决方法

COSMOS开发操作系统时switch模块构建错误解决

正在使用COSMOS开发操作系统,具备C#基础,但switch(cmd)模块出现构建错误。

switch代码片段

switch (cmd)
{
    case "mkdir":
        if (!string.IsNullOrWhiteSpace(arg))
        {
            FileOperations.CreateDirectory(arg);
            Console.WriteLine($"Directory '{arg}' created.");
        }
        else
        {
            Console.WriteLine("Usage: mkdir <directory>");
        }
        break;

    case "ls":
        FileOperations.ListFilesAndDirectories(filepath);
        break;

    case "rm":
        if (FileOperations.FileExists(arg))
        {
            FileOperations.DeleteFile(arg);
            Console.WriteLine($"File '{arg}' deleted.");
        }
        else if (FileOperations.DirectoryExists(arg))
        {
            FileOperations.DeleteDirectory(arg);
            Console.WriteLine($"Directory '{arg}' deleted.");
        }
        else
        {
            Console.WriteLine($"File or directory '{arg}' not found.");
        }
        break;

    case "ren":
        var renameArgs = arg.Split(' ', 2);
        if (renameArgs.Length == 2)
        {
            FileOperations.RenameFile(renameArgs[0], renameArgs[1]);
            Console.WriteLine($"Renamed '{renameArgs[0]}' to '{renameArgs[1]}'.");
        }
        else
        {
            Console.WriteLine("Usage: ren <oldname> <newname>");
        }
        break;

    case "cp":
        var copyArgs = arg.Split(' ', 2);
        if (copyArgs.Length == 2)
        {
            FileOperations.CopyFile(copyArgs[0], copyArgs[1]);
            Console.WriteLine($"Copied '{copyArgs[0]}' to '{copyArgs[1]}'.");
        }
        else
        {
            Console.WriteLine("Usage: cp <source> <destination>");
        }
        break;

    case "mv":
        var moveArgs = arg.Split(' ', 2);
        if (moveArgs.Length == 2)
        {
            FileOperations.MoveFile(moveArgs[0], moveArgs[1]);
            Console.WriteLine($"Moved '{moveArgs[0]}' to '{moveArgs[1]}'.");
        }
        else
        {
            Console.WriteLine("Usage: mv <source> <destination>");
        }
        break;

    default:
        Console.WriteLine($"Command '{cmd}' not recognized.");
        break;
}

FileOperations类代码

using Sys = Cosmos.System;

public class FileOperations
{
    public static void CreateFile(string path)
    {
        using (var file = File.Create(path)) { }
    }

    public static void WriteToFile(string path, string content)
    {
        File.WriteAllText(path, content);
    }

    public static string ReadFromFile(string path)
    {
        return File.ReadAllText(path);
    }

    public static void DeleteFile(string path)
    {
        File.Delete(path);
    }

    public static void CreateDirectory(string path)
    {
        Directory.CreateDirectory(path);
    }

    public static void DeleteDirectory(string path)
    {
        Directory.Delete(path, true);
    }

    public static bool FileExists(string path)
    {
        return File.Exists(path);
    }

    public static bool DirectoryExists(string path)
    {
        return Directory.Exists(path);
    }

    public static void RenameFile(string sourcePath, string destPath)
    {
        File.Move(sourcePath, destPath);
    }

    public static void CopyFile(string sourcePath, string destPath)
    {
        File.Copy(sourcePath, destPath, true);
    }

    public static void MoveFile(string file, string newpath)
    {
        File.Copy(file, newpath);
        File.Delete(file);
    }
    
    public static void ListFilesAndDirectories(string path)
    {
        if (!Directory.Exists(path))
        {
            Console.WriteLine($"Directory '{path}' not found.");
            return;
        }

        var directories = Directory.GetDirectories(path);
        var files = Directory.GetFiles(path);

        Console.WriteLine("Directories:");
        foreach (var dir in directories)
        {
            Console.WriteLine($"  {Path.GetFileName(dir)}");
        }

        Console.WriteLine("Files:");
        foreach (var file in files)
        {
            Console.WriteLine($"  {Path.GetFileName(file)}");
        }
    }
}

构建输出错误信息

1>------ 生成开始: 项目: OS, 配置: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\9.0.100-preview.7.24407.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(326,5): 消息 NETSDK1057: 你正在使用 .NET 的预览版本。
1>跳过分析器以加快生成速度。你可以执行“生成”或“重新生成”命令来运行分析器。
1>OS -> C:\Users\Kin\source\repos\OS\bin\Debug\net6.0\OS.dll
1>消息: 对程序集执行 IL2CPU
1>消息: 内核基础: Cosmos.System.Kernel
1>消息: 检查目标程序集: C:\Users\Kin\source\repos\OS\bin\Debug\net6.0\OS.dll
1>消息 : 警告 : 从程序集加载插件: Cosmos.Core_Asm, Version=10.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983
1>消息 : 警告 : 从程序集加载插件: Cosmos.Core_Plugs, Version=10.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983
1>消息 : 警告 : 从程序集加载插件: Cosmos.Debug.Kernel.Plugs.Asm, Version=10.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983
1>消息 : 警告 : 从程序集加载插件: Cosmos.System2_Plugs, Version=10.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983
1>IL2CPU : 错误 : 异常: System.Exception:
1>遇到本机代码,需要插件。
1>  请勿将此报告为 bug。
1>  需要插件: System.Boolean  Interop+Kernel32.MoveFileExPrivate(System.String, System.String, System.UInt32)(插件签名: System_Boolean__Interop_Kernel32_MoveFileExPrivate_System_String__System_String__System_UInt32_ )。
1>  静态: True
1>  程序集: System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
1>  调用路径:
1>Interop+Kernel32::Boolean MoveFile(System.String, System.String, Boolean)
1>System.IO.FileSystem::Void MoveFile(System.String, System.String, Boolean)
1>System.IO.File::Void Move(System.String, System.String, Boolean)
1>System.IO.File::Void Move(System.String, System.String)
1>FileOperations::Void RenameFile(System.String, System.String)
1>OS.Kernel::Void ExecuteCommand(System.String)
1>OS.Kernel::Void Run()
1>Cosmos.System.Kernel::Void Run()
1>
1>
1>   在 Cosmos.IL2CPU.ILScanner.ScanMethod(MethodBase aMethod, Boolean aIsPlug, String sourceItem) 位置 C:\Development\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:行 625
1>   在 Cosmos.IL2CPU.ILScanner.ScanQueue() 位置 C:\Development\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:行 816
1>   在 Cosmos.IL2CPU.ILScanner.Execute(MethodBase aStartMethod, IEnumerable`1 plugsAssemblies) 位置 C:\Development\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:行 276
1>   在 Cosmos.IL2CPU.CompilerEngine.Execute() 位置 C:\Development\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\CompilerEngine.cs:行 223
1>IL2CPU 任务耗时 00:00:01.0520069
1>项目“OS.csproj”的生成 -- 失败。
========== 生成: 0 成功,1 失败,0 最新,0 跳过 ==========
========== 生成完成于 3:12 下午,耗时 01.271 秒 ==========

解决方案

错误根源是File.Move方法调用了Windows系统底层接口,而COSMOS并未提供该接口对应的插件实现,导致IL2CPU编译失败。

修改FileOperations类中的RenameFile方法,改用已实现的复制+删除逻辑(和MoveFile方法保持一致),绕过对系统原生接口的依赖:

public static void RenameFile(string sourcePath, string destPath)
{
    // 替换原File.Move调用,使用COSMOS支持的API实现重命名
    File.Copy(sourcePath, destPath);
    File.Delete(sourcePath);
}

修改后重新构建即可解决该错误。

内容的提问来源于stack exchange,提问作者Minh Khôi Nguyễn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 00:40:54