将文件移动到不同的位置 [英] move files to different location

查看:80
本文介绍了将文件移动到不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,这是我将几个文件合并到一个文件的代码。

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.IO;


命名空间 ConsoleApplication2
{
class 程序
{
静态 void Main( string [] args)
{
string path = D:/ Project /;
// string [] fileNames = {A_L_PRTD_432124.txt,A_L_PRTD_021345.txt};
string fileNameStartsWIth = A_L_PRTD_ ;
string [] fileNames = Directory
.GetFiles(path, * .txt,SearchOption.AllDirectories)
.Select(f = > Path.GetFileName( f))。其中(k = > k.StartsWith(fileNameStartsWIth))。ToArray();

string finalFile = L_PRTD _ + DateTime.Now.ToString( ddMMyyyy)+ 。txt;

List< string> lstLines = new List< string>();
foreach 字符串文件 in fileNames)
{
string [] content = File.ReadAllLines(path + file).Select(k = > k.Trim())。Distinct()。ToArray();
lstLines.AddRange(content);
}
var data = lstLines.Distinct();
File.AppendAllLines(path + finalFile,data);


}
}
}





我想搬家旧文件到不同的位置。怎么办?

解决方案

使用 File.Move 方法 [ ^ ]:

 List< string> lstLines =  new  List< string>(); 
string movedFilesPath = [选择路径到将文件移至]; // 选择路径
foreach string file in fileNames)
{
string [] content = File.ReadAllLines(path + file).Select(k = > k.Trim())。 。不同的()ToArray的();
lstLines.AddRange(content);
if (!File.Exists(movedFilesPath + file)) // 检查文件是否尚未移动
{
File.Move(path + file,movedFilesPath + file); // 如果尚未移动,请将其移动
}
}


hello this is the code where i am combining few files to a single file .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "D:/Project/";
            //string[] fileNames = { "A_L_PRTD_432124.txt", "A_L_PRTD_021345.txt" };
            string fileNameStartsWIth = "A_L_PRTD_";
            string[] fileNames = Directory
                 .GetFiles(path, "*.txt", SearchOption.AllDirectories)
                 .Select(f => Path.GetFileName(f)).Where(k => k.StartsWith(fileNameStartsWIth)).ToArray();

            string finalFile = "L_PRTD_" + DateTime.Now.ToString("ddMMyyyy") + ".txt";

          List<string> lstLines = new List<string>();
            foreach (string file in fileNames)
            {
                string[] content = File.ReadAllLines(path + file).Select(k => k.Trim()).Distinct().ToArray();
                lstLines.AddRange(content);
            }
            var data = lstLines.Distinct();
            File.AppendAllLines(path + finalFile, data);


        }
    }
}



I want to move the old files to a different location . how to do ?

解决方案

Use the File.Move method[^]:

List<string> lstLines = new List<string>();
string movedFilesPath = "[Choose path to move files to]"; // choose a path
foreach (string file in fileNames)
{
    string[] content = File.ReadAllLines(path + file).Select(k => k.Trim()).Distinct().ToArray();
    lstLines.AddRange(content);
    if (!File.Exists(movedFilesPath + file)) // check whether the files are not yet moved
    {
        File.Move(path + file, movedFilesPath + file); // if they are not yet moved, move them
    }
}


这篇关于将文件移动到不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆