询问用户导出txt文件的路径c#

我正在尝试制作一个允许用户将数据导出到 txt 的程序,但是我想首先询问用户创建 txt 的路径是什么。但是我没有正确使用替换功能,我正在添加一个变量。对不起,如果这是一个愚蠢的问题。


我尝试过的:


string path = @"##Insert##\export.txt";


Console.WriteLine("Insert the path to export txt: ");

string temp = Console.ReadLine();

path = path.Replace($"##Insert##", "{temp}");


人到中年有点甜
浏览 183回答 3
3回答

慕森王

不要那样做,使用 Path 类来操作文件路径。假设您在 dir 中获得目录。(通过提示、配置、...)var&nbsp;dir&nbsp;=&nbsp;<path&nbsp;here>; var&nbsp;fullPath&nbsp;=&nbsp;Path.Combine(dir,&nbsp;"export.txt");

慕尼黑的夜晚无繁华

在替换的代码的最后一行path = path.Replace($"##Insert##", "{temp}");用path = path.Replace($"##Insert##", temp);

斯蒂芬大帝

我建议不要使用Replace方法,但使用String.Format替代string path = @"{0}\export.txt";Console.WriteLine("Insert the path to export txt: ");string temp = Console.ReadLine();path = String.Format(path, temp);但这只是一种适用于您的特定情况的解决方法,您宁愿使用 Path.Combine
打开App,查看更多内容
随时随地看视频慕课网APP