【问题标题】:How to create a file in a specified path in Ubuntu using C# [closed]如何使用 C# 在 Ubuntu 中的指定路径中创建文件 [关闭]
【发布时间】:2021-06-23 10:22:28
【问题描述】:

我有一个程序在 Ubuntu 系统中作为基于 Linux 的容器运行。该项目是使用 .NetCore 库编写的。

从我在 Ubuntu 的“根”目录中,我可以设置如下路径并查看那里的现有文件

root@user: cd /var/abc/modules/moduleOne/files

在该路径中存在一个名为“logs.txt”的文件

如何复制该文件并以不同的名称粘贴?

我尝试如下

var destination = "//var//abc//modules//moduleOne//files//copy.txt";
var source = Path.Combine("//var//abc//modules//moduleOne//files", "logs.txt");
File.Copy(source, destination);

它在运行时抛出如下错误

Exception occurred Could not find a part of the path '/var/abc/modules/moduleOne/files/logs.txt'

【问题讨论】:

  • 你为什么要写两次斜线?仅当您使用反斜杠时才需要这样做,因为您必须转义它们。
  • 首先检查文件是否存在并具有所需的权限 "docker exec -it [replace this with container name or id] ls -lah /var/abc/modules/moduleOne/files" 。第二次尝试去掉那些双斜杠(在你的代码中)。我的猜测是用户问题。在您的主机中,您通常使用您的用户名,而在 docker 中,您可以在图像或容器上指定用户。
  • 我误写了双斜线。确实单斜线工作正常

标签: c# linux docker ubuntu copy-paste


【解决方案1】:

我已经在 docker linux 容器中测试了以下代码,它可以工作:

File.WriteAllText("/test.txt", "Test");
string destination = "/var/copy.txt";
string source = Path.Combine("/", "test.txt");
System.IO.File.Copy(source, destination);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2023-03-21
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多