C# 如何使用 StreamWriter 创建目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/530245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a directory using StreamWriter?
提问by theKing
Is it possible to create a directory using StreamWriter?
是否可以使用 StreamWriter 创建目录?
回答by Chris Hynes
No. You can't actually create a directory using a StreamWriter. Use Directory.CreateDirectoryinstead.
不可以。您实际上无法使用 StreamWriter 创建目录。请改用Directory.CreateDirectory。
If you're trying to read the directory name out of a file stream and then create a file based on that text, you'll need something like this:
如果您尝试从文件流中读取目录名称,然后根据该文本创建一个文件,您将需要如下内容:
FileStream fs; // this is the filestream from somewhere. make sure to dispose it
using (StreamReader r = new StreamReader(fs))
Directory.CreateDirectory(r.ReadToEnd());