有没有办法在C#中使用字符串名称创建文本文件?

0 c# string filestream streamwriter text-files

我不确定是否可能.我通常创建这样的txt文件:

FileStream fs = new FileStream("c:\\textFile.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Run Code Online (Sandbox Code Playgroud)

但不是使用"c:\ textFile.txt"我想使用字符串名称创建一个文件.有办法吗?

Mic*_*tta 5

当然.FileStream构造函数的第一个参数采用字符串.您刚刚传递了一个字符串文字(在源代码文件中定义).听起来你想传递一个字符串变量:

string path = // get string from somewhere. A file save dialog, maybe?
FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Run Code Online (Sandbox Code Playgroud)

我建议您花一些时间学习C#教程吗?微软有一些很好的教程示例.尽管如此,我们曾经都在那里,你的知识中有一些漏洞会让你失望.