C# 正确使用string.format在WinC窗体中显示字符串#

C# 正确使用string.format在WinC窗体中显示字符串#,c#,winforms,C#,Winforms,我有一个窗口窗体,在该窗体中显示listview中的所有目录。对于复制文件,如果一个文件已经存在,它将再次复制该文件,并将file.txt(1)添加到该文件中。如果再次复制该文件,则将file.txt(2) string fileNameOnly = Path.GetFileNameWithoutExtension(file); string extension = Path.GetExtension(file); string pathDir = Path.GetDirectoryName(f

我有一个窗口窗体,在该窗体中显示listview中的所有目录。对于复制文件,如果一个文件已经存在,它将再次复制该文件,并将file.txt(1)添加到该文件中。如果再次复制该文件,则将file.txt(2)

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");

但问题是,当我一次又一次地复制这些文件时,模式就像file.txt(1)/file.txt(1)(1)/file.txt(1)(1)/file.txt(1)(1)/里面的数字不会增加。我不知道每次复制时如何增加计数。有人能告诉我做错了什么吗

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);

{}内的任何内容都是占位符。您使用了{(1)},它将始终为1。

code>count++表示变量在求值后递增。您可能希望这样:

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
string tempFileName = string.Format("{0}({1})", fileNameOnly, ++count);
int count = 0;
string fullPath = file;
while (File.Exists(fullPath))
{
    string fileName = Path.GetFileName(file);
    string extension = Path.GetExtension(fileName);
    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
    int lastIndexOfOpenBracket = fileNameWithoutExtension.LastIndexOf('(');
    string fileNameWithoutNumber = fileNameWithoutExtension.Substring(0, lastIndexOfOpenBracket);
    fileNameWithoutExtension = string.Format("{0}({1})", fileNameWithoutNumber, ++count);
    fullPath = Path.Combine(DestinationFolder, fileNameWithoutExtension + extension);
}
if (MessageBox.Show(file + " already exists\r\nDo you want to copy Again?",
        "Overwrite", MessageBoxButtons.OKCancel,
        MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    File.Copy(file, fullPath);
    MessageBox.Show("File Copied");
}
(请注意,我还将括号移到了括号外)

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
编辑

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
另外,您总是使用完整的文件名(不带扩展名)来创建新文件。但是您应该用旧的数字+1替换旧的数字

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
请尝试以下方法:

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
string tempFileName = string.Format("{0}({1})", fileNameOnly, ++count);
int count = 0;
string fullPath = file;
while (File.Exists(fullPath))
{
    string fileName = Path.GetFileName(file);
    string extension = Path.GetExtension(fileName);
    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
    int lastIndexOfOpenBracket = fileNameWithoutExtension.LastIndexOf('(');
    string fileNameWithoutNumber = fileNameWithoutExtension.Substring(0, lastIndexOfOpenBracket);
    fileNameWithoutExtension = string.Format("{0}({1})", fileNameWithoutNumber, ++count);
    fullPath = Path.Combine(DestinationFolder, fileNameWithoutExtension + extension);
}
if (MessageBox.Show(file + " already exists\r\nDo you want to copy Again?",
        "Overwrite", MessageBoxButtons.OKCancel,
        MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    File.Copy(file, fullPath);
    MessageBox.Show("File Copied");
}

你犯了一个简单的错误

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);

将{(1)}替换为({1})

这次我已经修改了你的代码格式,但以后请在发布之前整理好你的问题…(该死…Jon Skeet只比我的编辑快了一秒钟。他甚至编辑得很快!)你确定{(1)}总是1吗?它会显示file.txt(1)(2)(2)当我第三次复制它时time@engineer41:你真的使用了完整的代码吗?我改变了一切<代码>文件是文件的完整路径吗?@engineer41:重试。我忘了用新零件替换旧零件。看看
lastIndexOfOpenBracket
part.ys,但我们仍然没有检查条件,如果(conatain 1在下一个索引处,然后将其增加到2,就像它的2将其增加到3一样。我们增加的是(1)…n在第二次编辑后,它会这样做…f.txt(1)…f.txt(1)(2)…f.txt(1)(1)(2)让我们
string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");