C# 在C中检查文件、创建文件夹和移动文件#
我想创建一个新文件夹时没有问题 我和你一起工作 Directory.CreateDirectory 现在,我正在尝试从桌面获取所有图像文件,并希望将所有图像移动到使用Directory.CreateDirectory创建的文件夹中 我已经测试了file.MoveTo 从这里C# 在C中检查文件、创建文件夹和移动文件#,c#,C#,我想创建一个新文件夹时没有问题 我和你一起工作 Directory.CreateDirectory 现在,我正在尝试从桌面获取所有图像文件,并希望将所有图像移动到使用Directory.CreateDirectory创建的文件夹中 我已经测试了file.MoveTo 从这里 FileInfo file = new FileInfo(@"C:\Users\User\Desktop\test.txt"); 到这里 file.MoveTo(@"C:\Users\User\Desktop\folder
FileInfo file = new FileInfo(@"C:\Users\User\Desktop\test.txt");
到这里
file.MoveTo(@"C:\Users\User\Desktop\folder\test.txt");
这很好用。
现在我想用我的dekstop中的所有图像文件来实现这一点
(Directory.CreateDirectory(@"C:\Users\User\Desktop\Images");)
我该怎么做呢?请试试这个:您可以筛选特定目录中的文件,然后通过搜索结果移动每个文件,您可能可以修改搜索模式以匹配多种不同的图像文件格式
var files = Directory.GetFiles("PathToDirectory", "*.jpg");
foreach (var fileFound in files)
{
//Move your files one by one here
FileInfo file = new FileInfo(fileFound);
file.MoveTo(@"C:\Users\User\Desktop\folder\" + file.Name);
}
请试试这个:您可以筛选特定目录中的文件,然后通过搜索结果移动每个文件,您可能可以修改搜索模式以匹配多种不同的图像文件格式
var files = Directory.GetFiles("PathToDirectory", "*.jpg");
foreach (var fileFound in files)
{
//Move your files one by one here
FileInfo file = new FileInfo(fileFound);
file.MoveTo(@"C:\Users\User\Desktop\folder\" + file.Name);
}
从一个根文件夹获取具有特定扩展名的图像的示例代码:
static void Main(string[] args)
{
// path to desktop
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//get file extentions by speciging the needed extentions
var images = GetFilesByExtensions(new DirectoryInfo(desktopPath) ,".png", ".jpg", ".gif");
// loop thrue the found images and it will copy it to a folder (make sure the folder exists otherwise filenot found exception)
foreach (var image in images)
{
// if you want to move it to another directory without creating a copy use:
image.MoveTo(desktopPath + "\\folder\\" + image.Name);
// if you want to move a copy of the image use this
File.Copy(desktopPath + "\\"+ image.Name, desktopPath + "\\folder\\" + image.Name, true);
}
}
public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
if (extensions == null)
throw new ArgumentNullException("extensions");
var files = dir.EnumerateFiles();
return files.Where(f => extensions.Contains(f.Extension));
}
static void Main(字符串[]args)
{
//桌面路径
var desktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//通过指定所需的扩展名来获取文件扩展名
var images=GetFilesByExtensions(新的DirectoryInfo(desktopPath)、.png、.jpg、.gif);
//循环浏览找到的图像,并将其复制到文件夹中(确保文件夹存在,否则会出现filenot found异常)
foreach(图像中的var图像)
{
//如果要将其移动到另一个目录而不创建副本,请使用:
image.MoveTo(desktopath+“\\folder\\\”+image.Name);
//如果要移动图像的副本,请使用此
文件.Copy(desktopath+“\\”+image.Name,desktopath+“\\\”+folder\\“+image.Name,true);
}
}
公共静态IEnumerable GetFileByExtensions(目录信息目录,参数字符串[]扩展名)
{
if(扩展==null)
抛出新的ArgumentNullException(“扩展”);
var files=dir.EnumerateFiles();
返回文件。其中(f=>extensions.Contains(f.Extension));
}
从一个根文件夹获取具有特定扩展名的图像的示例代码:
static void Main(string[] args)
{
// path to desktop
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//get file extentions by speciging the needed extentions
var images = GetFilesByExtensions(new DirectoryInfo(desktopPath) ,".png", ".jpg", ".gif");
// loop thrue the found images and it will copy it to a folder (make sure the folder exists otherwise filenot found exception)
foreach (var image in images)
{
// if you want to move it to another directory without creating a copy use:
image.MoveTo(desktopPath + "\\folder\\" + image.Name);
// if you want to move a copy of the image use this
File.Copy(desktopPath + "\\"+ image.Name, desktopPath + "\\folder\\" + image.Name, true);
}
}
public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
if (extensions == null)
throw new ArgumentNullException("extensions");
var files = dir.EnumerateFiles();
return files.Where(f => extensions.Contains(f.Extension));
}
static void Main(字符串[]args)
{
//桌面路径
var desktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//通过指定所需的扩展名来获取文件扩展名
var images=GetFilesByExtensions(新的DirectoryInfo(desktopPath)、.png、.jpg、.gif);
//循环浏览找到的图像,并将其复制到文件夹中(确保文件夹存在,否则会出现filenot found异常)
foreach(图像中的var图像)
{
//如果要将其移动到另一个目录而不创建副本,请使用:
image.MoveTo(desktopath+“\\folder\\\”+image.Name);
//如果要移动图像的副本,请使用此
文件.Copy(desktopath+“\\”+image.Name,desktopath+“\\\”+folder\\“+image.Name,true);
}
}
公共静态IEnumerable GetFileByExtensions(目录信息目录,参数字符串[]扩展名)
{
if(扩展==null)
抛出新的ArgumentNullException(“扩展”);
var files=dir.EnumerateFiles();
返回文件。其中(f=>extensions.Contains(f.Extension));
}
Directory.GetFiles的可能重复项正常,但我想将所有图像从桌面移动到桌面上新创建的文件夹中?确定存在哪些文件符合您的条件(例如,某些文件扩展名)并移动它们。是什么阻止了你?是的,这正是我想做的,但我怎么能做到呢?这很简单。。。你可以在谷歌上找到这个。可能是Directory.GetFiles的副本。好的,但我想将所有图像从我的桌面移动到我的桌面上新创建的文件夹?确定存在哪些文件符合您的条件(例如,某些文件扩展名)并移动它们。是什么阻止了你?是的,这正是我想做的,但我怎么能做到呢?这很简单。。。你可以在谷歌上找到这个。谢谢!这正是我所需要的。但我已经用var files=Directory.GetFiles(@“C:\Users\User\Desktop”,“*.jpg”)尝试过了;foreach(var filefind in files){//在这里逐个移动您的文件FileInfo file=new FileInfo(filefind);file.MoveTo(@“C:\Users\User\Desktop\Images”);}我收到一个错误您收到的是哪种错误?您可以将其粘贴到注释中吗?System.IO.IOException是否已将用户替换为路径中的windows用户名?谢谢!这正是我所需要的。但我已经用var files=Directory.GetFiles(@“C:\Users\User\Desktop”,“*.jpg”)尝试过了;foreach(var filefind in files){//在这里逐个移动您的文件FileInfo file=new FileInfo(filefind);file.MoveTo(@“C:\Users\User\Desktop\Images”);}我收到一个错误您收到的是哪种错误?你能把它粘贴到评论中吗?System.IO.ioexception你在路径中用你的windows用户名替换了用户了吗?谢谢你!我就是这个意思。。。我没有收到任何错误,但我的文件夹仍然为空。为什么?哦,很抱歉,我没有使用字符串searchPattern=“.bmp”+“.jpg”;我用string searchPattern=“.bmp”+“.jpg”再次尝试了它;foreach(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),searchPattern)中的字符串img{FileInfo file=newfileinfo(img);file.MoveTo(@“C:\Users\User\Desktop\Images”);}我的文件夹仍然是空的,我不知道我在上面,请稍候,谢谢,但为什么是File.Copy(desktopath+“//”+image.Name,desktopPath+“//folderr//”+image.Name,true);不动吗?我不懂p