我正在尝试保存一些都具有相同名称的文件。我想做一些名字做这样的事情: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;