C# 在文件夹c中移动文本文件#
我有一个包含多个文本文件的文件夹,我想读取它们以获取某些行的内容,然后将该文件移动到“备份”文件夹 我的“备份”文件夹的路径是: \SRVWEB001\spool\u接口\PVCHARGE\Backup\ 包含我的文本文件的路径: \SRVWEB001\spool\u接口\PVCHARGE\UTS600\ 我设法读取了文件的行并将它们存储在列表中,但当我想将文本文件复制到“备份”文件夹中时,我无法复制它们,因为我出现了以下错误:“无法创建现有文件。”(System.IO.IO例外),但我的文件在该文件夹中尚不存在 这是我的代码:C# 在文件夹c中移动文本文件#,c#,C#,我有一个包含多个文本文件的文件夹,我想读取它们以获取某些行的内容,然后将该文件移动到“备份”文件夹 我的“备份”文件夹的路径是: \SRVWEB001\spool\u接口\PVCHARGE\Backup\ 包含我的文本文件的路径: \SRVWEB001\spool\u接口\PVCHARGE\UTS600\ 我设法读取了文件的行并将它们存储在列表中,但当我想将文本文件复制到“备份”文件夹中时,我无法复制它们,因为我出现了以下错误:“无法创建现有文件。”(System.IO.IO例外),但我的文件在
[WebMethod]
public string enTete()
{
// Stocke une à une les lignes du fichier
List<string> allLines = new List<string>();
// Stocke l'en-tête
List<string> enTete = new List<string>();
// Chemin du dossier contenant les fichiers
string dirPath = @"\\SRVWEB001\spool_interface\PVCHARGE\UTS600\";
DirectoryInfo d = new DirectoryInfo(dirPath);
// Si il existe des fichiers txt dans le dossier
if (Directory.GetFiles(dirPath, "*.txt").Length != 0)
{
// Pour chaque fichier
foreach (var fichier in d.GetFiles("*.txt"))
{
// Lis le fichier
using (FileStream fs = fichier.OpenRead())
{
byte[] b = new byte[fs.Length];
int counter = 0;
string line;
StreamReader file = new StreamReader(fs, System.Text.Encoding.GetEncoding("iso-8859-1"));
// Récupération de chaque ligne dans la List<string> allLines
while ((line = file.ReadLine()) != null)
{
allLines.Add(line);
counter++;
}
// Fermeture du fichier
file.Close();
// Dossier "Backup"
string backupPath = @"\\SRVWEB001\spool_interface\PVCHARGE\Backup\";
DirectoryInfo d2 = new DirectoryInfo(backupPath);
// Fichier à déplacer
string filePath = dirPath + fichier.ToString();
if (Directory.Exists(dirPath))
{
string[] files = Directory.GetFiles(dirPath);
foreach (string s in files)
{
// Déplace le fichier dans le dossier "Backup"
File.Move(s, backupPath);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Récupération des colonnes de l'en-tête dans un tableau (correspond à la ligne 105)
char[] separator = new char[] { '\t' };
string[] colonnes = allLines[105].Split(separator, StringSplitOptions.None);
// Affichage des colonnes
return colonnes[0]+" "+colonnes[1]+" "+colonnes[2]+" "+colonnes[3]+" "+colonnes[4]+" "+colonnes[5]+" "+colonnes[6] + " " +colonnes[7] + " " +colonnes[8] + " " +colonnes[9]+" "+ colonnes[10] + " " + colonnes[11] + " " + colonnes[12] + " " + colonnes[13] + " " + colonnes[14];
}
}
return "Succès";
}
else
{
return "Pas de fichier texte";
}
}
[WebMethod]
公共字符串enTete()
{
//在菲舍尔的树林里
List allLines=新列表();
//Stocke l'en-tête
List enTete=新列表();
//档案管理人员
字符串dirPath=@“\\SRVWEB001\spool\u interface\PVCHARGE\UTS600\”;
DirectoryInfo d=新的DirectoryInfo(dirPath);
//你可以在档案中找到你的名字
if(Directory.GetFiles(dirPath,“*.txt”).Length!=0)
{
//倒酒
foreach(d.GetFiles(“*.txt”)中的变量fichier)
{
//费希尔酒店
使用(FileStream fs=fichier.OpenRead())
{
字节[]b=新字节[fs.Length];
int计数器=0;
弦线;
StreamReader文件=新的StreamReader(fs,System.Text.Encoding.GetEncoding(“iso-8859-1”);
//请列出所有行
而((line=file.ReadLine())!=null)
{
所有行。添加(行);
计数器++;
}
//费希尔假日酒店
file.Close();
//档案“备份”
字符串backupPath=@“\\SRVWEB001\spool\u interface\PVCHARGE\Backup\”;
DirectoryInfo d2=新的DirectoryInfo(备份路径);
//菲希耶砂矿
字符串filePath=dirPath+fichier.ToString();
if(Directory.Exists(dirPath))
{
string[]files=Directory.GetFiles(dirPath);
foreach(文件中的字符串s)
{
//Déplace le fichier dans le卷宗“备份”
文件。移动(s,后退);
}
}
其他的
{
WriteLine(“源路径不存在!”);
}
//舞台上的柱状图(对应于第105页)
字符[]分隔符=新字符[]{'\t'};
string[]colonnes=allLines[105]。拆分(分隔符,StringSplitOptions.None);
//科隆学院
返回冒号[0]+“+colonnes[1]+”+colonnes[2]+“+colonnes[3]+”+colonnes[4]+“+colonnes[5]+”+colonnes[6]+“+colonnes[7]+”+colonnes[8]+“+colonnes[9]+”+colonnes[10]+“+colonnes[11]+”+colonnes[12]+“+colonnes[13]+”+colonnes[14];
}
}
返回“成功”;
}
其他的
{
返回“Pas de fichier texte”;
}
}
提前感谢您的帮助您需要为File.Move提供文件名,而不是directoy名称。而不是:
File.Move(s, backupPath);
试一试
您需要为File.Move提供一个文件名,而不是directoy名称。而不是:
File.Move(s, backupPath);
试一试
File.Move的第二个参数必须是要将文件移动到的路径和文件名。出现异常的原因是,在不包含文件名的情况下,您试图移动文件并创建与备份文件夹同名的文件。file.move的第二个参数必须是要将文件移动到的路径和文件名。出现异常的原因是,在不包含文件名的情况下,您正在尝试移动文件并创建与备份文件夹同名的文件。是否可以读取多个文本文件并在“我的文件夹”中逐个移动它们?因为我的代码只适用于一个文件,而不适用于我文件夹中的所有文件,所以我不明白它应该起什么作用,但打开的文件很容易让你遇到麻烦。我建议您不要使用.StreamReader和/或.OpenRead,除非您有非常大的文件。请尝试File.ReadAllLines/File.writeallines等,这样您就不必记得正确关闭文件。是否可以读取多个文本文件并在我的文件夹中逐个移动它们?因为我的代码只适用于一个文件,而不适用于我文件夹中的所有文件,所以我不明白它应该起什么作用,但打开的文件很容易让你遇到麻烦。我建议您不要使用.StreamReader和/或.OpenRead,除非您有非常大的文件。尝试File.ReadAllLines/File.writeallines等,这样您就不必记得正确关闭文件了。