我正在尝试保存一些名称相同的文件。我想做一些名字做的事情:file.extension file [1] .extension file [2] .extension我试过这个http://www.naspinski.net/post/Saving-multiple-files-of-the-same-name.aspx但它对我不起作用。
这里有一些要查看的代码(不是整个事情,只是相关部分),
{
string thepathoflife = Path.GetFullPath(file);
//CreatetheFolder(file)
string filetocopy = file;
object bob = file.Clone();
string bobby = bob.ToString();
string location = file;
bool b = false;
string extension = Path.GetExtension(file);
string thenameofdoom = Path.GetFileNameWithoutExtension(file);
string filename = Path.GetFileName(file);
////bobby.Move(@"\\TEST12CVG\Public\Posts\Temporaryjunk" + filename);
// string oldlocation = filename+extension;
if (extension == ".pst" ||
extension == ".tec" ||
extension == ".pas" ||
extension == ".snc" ||
extension == ".cst" ||
extension == ".xml")
{
b = true;
}
if (thenameofdoom == "Plasma" ||
thenameofdoom == "Oxygas" ||
thenameofdoom == "plasma" ||
thenameofdoom == "oxygas" ||
thenameofdoom == "Oxyfuel" ||
thenameofdoom == "oxyfuel")
{
b = false;
}
if (b == true)
// System.IO.File.WriteAllText(newlocation, bobby);
{
//string rootpath = (@"\\sigmatek.net\Documents\Customers\A");
var findLevel = 6;
var path = @thepathoflife;
var levels = path.Split(Path.DirectorySeparatorChar);
var second = levels.Length > findLevel ? levels[findLevel] : null;
Directory.CreateDirectory(@"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom);
string newlocation = (@"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom);
string newPath = System.IO.Path.Combine(newlocation, second);
System.IO.Directory.CreateDirectory(newPath);
string newlocationb = Path.GetFullPath(newPath);
string newb = System.IO.Path.Combine(newlocationb, filename);
while (File.Exists(newb))
{
int number = 1;
bool found = false;
do
{
string candidate = newb.Replace(extension, "[" + number++ + "]"+ extension);
if (!File.Exists(candidate)) found = true;
{
File.Copy(thepathoflife, candidate);
}
// Candidate has a valid file name
}
}
//File.Move(@"\\TEST12CVG\Public\Posts\Test\", @"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom + second);
System.Console.WriteLine("Success: " + filename + "--" + thepathoflife);
b = false;
答案 0 :(得分:1)
这是我的头脑。此外,如果“.extension”出现在文件名中的某个位置而不是最后,则会中断(因此使您的字符串处理比示例代码更聪明)。如果需要,您可以使用Path.GetExtension(path)
if (File.Exists(fn))
{
int number = 1;
bool found = false;
do
{
string candidate = fn.Replace(".extension", "[" + number++ + "].extension");
if (!File.Exists(candidate)) found = true;
}
// Candidate has a valid file name
}
答案 1 :(得分:1)
插入“ sPathAndFileName ”并使用输出的“ sSaveName ”保存文件。
您将看到友好递增的文件名,如:Test.txt,Text [1] .txt,Test [2] .txt等...
int index = 0;
string sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
+ Path.GetFileNameWithoutExtension(sPathAndFileName)
+ Path.GetExtension(sPathAndFileName);
while (File.Exists(sSaveName) == true)
{
sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
+ Path.GetFileNameWithoutExtension(sPathAndFileName)
+ "[" + (++index).ToString() + "]"
+ Path.GetExtension(sPathAndFileName);
}
答案 2 :(得分:1)
试试这个。 更简单。
string filename = "C:\bla.txt";
string filenameNoPath = Path.GetFileNameWithoutExtension(filename);
string temppath = Path.GetDirectoryName(filename);
string extension = Path.GetExtension(filename);
if (!File.Exists(path))
{
File.WriteAllBytes(filename, file);
}
else
{
do
{
counter++; // we're here, so lets count files with same name
string path = temppath + "\\" + filenameNoPath + "(" + counter.ToString() + ")" + extension;
} while (File.Exists(path));
File.WriteAllBytes(path, file);
}
答案 3 :(得分:0)
可能你需要使用Path.GetExtension(string path)
来获取文件的扩展名,之后只需剪切文件名,就会在其上附加迭代编号并再次扩展。
答案 4 :(得分:0)
您不能将具有相同名称和扩展名的文件放在同一文件夹中,因此您可以选择以下选项:
将它们全部放在具有有意义名称的单独目录中(即使文件具有相同的名称,应该是不同的)
使用前面的日期时间戳,比如每个文件前面的YYYYMMDD(但它违反了“相同的文件名”规则)
同样可以应用于扩展程序
答案 5 :(得分:0)
我不禁觉得这些年来我做过这么多次。 :)
尝试以下几点:
List<string> filesToSave = new List<string>();
var fileName = "myfile.ext";
var baseName = Path.GetFileNameWithoutExtension(fileName);
var extension = Path.GetExtension(fileName);
int counter = 1;
foreach (var fileToSave in filesToSave)
{
var tempFileName = fileName;
while (File.Exists(tempFileName))
{
tempFileName = string.Format("{0}{1}.{2}", baseName, counter, extension);
counter++
}
File.WriteAllText(tempFileName, fileToSave);
}
答案 6 :(得分:0)
以下是一个如何帮助自己的例子:
string[] files = { "file.txt", "file.txt", "file.txt" };
for(int i=0;i<files.Length;i++)
{
string fileName = Path.GetFileNameWithoutExtension(files[i]);
string extension = Path.GetExtension(files[i]);
fileName = fileName + (i + 1);
//if you would like to aqdd square brackets, you can change upper line to:
fileName = string.Format("{0}{1}{2}{3}", fileName, "[", (i + 1), "]");
files[i] = String.Concat(fileName, extension);
}
如果你有FileInfo数组中的文件,你可以这样做。