首页 > 解决方案 > 询问用户在 C# 控制台上导出文件的路径

问题描述

我是编程新手,我正在尝试将创建 txt 文件的路径添加到字符串变量中。但是,如果可以使用 Replace 或 Concat 完成,我做错了。我从未在 C# 上使用过它。这是我到目前为止所做的:

string path = @"###";
do
{
    Console.Write("Insert the path in oder to export data: ");
    string temp = Console.ReadLine();
} 
while (String.IsNullOrEmpty(path));

path = path.Replace("###", "temp"); 

标签: c#filereplace

解决方案


以下行

path = path.Replace("###", "temp"); 

###用文字字符串替换路径中的部分temp

在操作结束时,变量的内容将为“temp”。path

你根本不需要做 a Replace。反而,

string path = string.Empty;
do
{
    Console.Write("Insert the path in oder to export data: ");
    path = Console.ReadLine();
} 
while (String.IsNullOrEmpty(path));

将用户输入的路径分配到您的path变量中。


推荐阅读