问题
I am trying to save a few files that all have the same name. I want to do something where the names do something like this: file.extension file[1].extension file[2].extension I have tried this http://www.naspinski.net/post/Saving-multiple-files-of-the-same-name.aspx but it didn't work for me.
here is some code to look at(not the entire thing, just the relevant parts),
{
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;
回答1:
This is off the top of my head. Also, if ".extension" occurs somewhere in the filename other than at the end, this would break (so make your string handling a bit smarter than the sample code). If needed, you can get the extension using 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
}
回答2:
Plug in "sPathAndFileName" and use the outputted "sSaveName" to save your file.
You will see friendly incremented file names like: Test.txt, Text[1].txt, Test[2].txt, etc...
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);
}
回答3:
Try this. Much simplier.
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);
}
回答4:
Probably you need to use the Path.GetExtension(string path) in order to get the extension of the file and after that just cut the filename append the iteration number on it and extension again.
回答5:
You can not put files with the same name and extension in the same folder, so you have an options like:
put them all in seprate directories wih meaningful names (even if files have a same name the content should be digferent)
use date time stamp in front, like YYYYMMDD in front of every file (but it violates the "same file name" rule)
the same can be applied to extensions
回答6:
I can't help but feel I did this too many times over the years. :)
Try something along these lines:
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);
}
回答7:
Here is an example how you can help your self:
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);
}
Same you can do, if you have files in FileInfo array.
来源:https://stackoverflow.com/questions/10356458/multiple-files-same-name