wpf 使用 System.IO.File.Move 时自动创建文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25201088/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:18:25  来源:igfitidea点击:

Auto creating folders when using System.IO.File.Move

c#wpfwinformsmove.net

提问by roryok

I'm updating an old winforms app which moves files to new locations using regex and System.IO.File.Move

我正在更新一个旧的 winforms 应用程序,它使用正则表达式和 System.IO.File.Move 将文件移动到新位置

Under windows 7, the old app worked fine. If a folder didn't exist, File.Move would create it

在 Windows 7 下,旧的应用程序运行良好。如果文件夹不存在,File.Move 会创建它

System.IO.File.Move("c:\stuff\a.txt","c:\stuff\a\file.txt");
System.IO.File.Move("c:\stuff\b.txt","c:\stuff\b\file.txt");
System.IO.File.Move("c:\stuff\c.txt","c:\stuff\c\file.txt");

However, under Windows 8 it seems that I have to manually create each folder in the path first. I get an error if I try and move to a folder that doesn't exist yet. Anyone know a way around this? I'd rather not have to create each folder

但是,在 Windows 8 下,我似乎必须先在路径中手动创建每个文件夹。如果我尝试移动到尚不存在的文件夹,则会收到错误消息。任何人都知道解决这个问题的方法吗?我宁愿不必创建每个文件夹

NOTE: The new, updated app is on WPF rather than winforms. Not sure if that's relevant

注意:新的、更新的应用程序在 WPF 上而不是在 winforms 上。不确定这是否相关

回答by Craig

Before you File.Move()you could do:

File.Move()你可以做之前:

new System.IO.FileInfo("c:\stuff\a\file.txt").Directory.Create();

The above will create the "stuff" and "a" folders if they don't exist.

如果它们不存在,上面将创建“stuff”和“a”文件夹。