只是一个简单的问题.我正在使用这样的东西
FileStream fs = new FileStream(fileName, FileMode.Create);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一个参数我可以传递给它以强制它创建文件夹,如果它不存在.如果找不到文件夹,则抛出异常.
如果有更好的方法,那么使用FileStream我是开放的想法.
dtb*_*dtb 119
Directory.CreateDirectory方法(String)
创建path指定的所有目录和子目录.
例:
string fileName = @"C:\Users\SomeUser\My Documents\Foo\Bar\Baz\text1.txt";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
(Path.GetDirectoryName返回文件名的目录部分.)
Joe*_*Joe 15
就像是:
void EnsureFolder(string path)
{
string directoryName = Path.GetDirectoryName(path);
// If path is a file name only, directory name will be an empty string
if (directoryName.Length > 0)
{
// Create all directories on the path that don't already exist
Directory.CreateDirectory(directoryName);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63398 次 |
| 最近记录: |