gpt4 book ai didi

c# - 多个文件,同名

转载 作者:行者123 更新时间:2023-11-30 22:29:07 27 4
gpt4 key购买 nike

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

最佳答案

这超出了我的想象。此外,如果“.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
}

关于c# - 多个文件,同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com