.NET 7 wasm 在 Linux 中看不到文件

问题描述 投票:0回答:1

我试图通过 dotnet workload install wasm-experimental

dotnet new wasmconsole
使用
Microsoft 指南
设置从 node.js 运行 .NET。

我想从.NET读取vsdx文件,但出现错误:

读取文件时出错:找不到路径的一部分 '/home/user/project/src/file.vsdx'

.Net 应用程序:

using System;
using System.IO;
using System.Runtime.InteropServices.JavaScript;
using System.IO.Packaging;
using System.Linq;

return 0;
public partial class MyClass
{
  [JSExport]
  internal static string Greeting()
  {
    try {
      string filePath = "/empty-template.vsdx";

      if (File.Exists(filePath))
      {
        Console.WriteLine("Файл существует по пути: " + filePath);
      }
      else
      {
        Console.WriteLine("Файл не существует по пути: " + filePath);
      }

      try {
        File.ReadAllBytes(filePath);
      }
      catch (Exception ex) {
        Console.WriteLine($"Error reading file: {ex.Message}");
      }
    } catch (Exception ex) {
      Console.WriteLine($"Error opening package: {ex.Message}");
    }

    return "filePath";
  }

文件按路径存在。

node.js .net asp.net-core ubuntu .net-assembly
1个回答
0
投票

filePath
应使用相对路径,而不是绝对路径。你应该像下面这样改变它:

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string filePath = Path.Combine(baseDirectory, "src", "empty-template.vsdx");
© www.soinside.com 2019 - 2024. All rights reserved.