如果文件存在,我如何正确地自动追加到文件名

本文关键字:追加 文件名 正确地 存在 文件 如果 | 更新日期: 2023-09-27 18:15:59

这是我尝试过的,请注意lblImageAlt.Text属性已设置为'Images/'

        string ImgPath1 = lblImageAlt1.Text.ToString();
        string ImgPath2 = lblImageAlt2.Text.ToString();
        string ImgPath3 = lblImageAlt3.Text.ToString();
        string filename1 = "";
        string filename2 = "";
        string filename3 = "";

        if (fileuploadimages1.HasFile)
        {
            if (File.Exists(Server.MapPath(ImgPath1 + filename1)))
            {
                string extension = Path.GetExtension(filename1);
                string name = Path.GetFileNameWithoutExtension(filename1);
                int fileMatchCount = 1;
                while (File.Exists(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension)))
                    fileMatchCount++;
                fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension));
            }
            else
            {
                fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));
            }
        }
        else
        {
            filename1 = "noImage.jpg";
        }

,但相同的图像没有得到一个数字追加到它。我哪里做错了?

如果文件存在,我如何正确地自动追加到文件名

路径。GetFileName返回带有扩展名的整个文件名。
因此,您的代码正在检查文件名是否存在:

 image.jpg1

您应该更改代码将文件名分成两部分,基本文件名和扩展名,然后检查文件名是否存在,然后从其部分重新构建文件名,并添加增量数,直到找到不存在的文件名

// Extract just the filename from the posted file removing the path part (image.jpg)
filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);
baseFile = Path.GetFileNameWithoutExtension(fileuploadimages1.PostedFile.FileName);
extension = Path.GetExtension(fileuploadimages1.PostedFile.FileName);
int fileMatchCount = 1;
// Check if a file with the given name exists in the Images1 subfolder of the root folder of your site
while(File.Exists(Server.MapPath(Path.Combine(ImgPath1, filename1)))
{
    // The given file exists already, so we now need to build
    // a different (but related) filename using a counter....
    // This will create a filename like 'image(001).jpg'
    // and then we will restart the loop        
    fileName1 = string.Format("{0}({1:D3}){2}", baseFile, fileMatchCount, extension);
    // ... but first increment the counter in case even the new name exists
    fileMatchCount++;
}
// We exit the loop with a name that should not exists in the destination folder
fileuploadimages1.SaveAs(Server.MapPath(Path.Combine(ImgPath1, filename1));

实际上并没有修改filename1。您正在检查它是否以(0),(1)等结束,并增加索引,但从未实际修改变量。

尝试使用

    if(File.Exists(Server.MapPath(ImgPath1 + filename1)))
{
    string extension = Path.GetExtension(filename1);
    string name = Path.GetFileNameWithoutExtension(filename1);
    int fileMatchCount = 1;
    while(File.Exists(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension)))
        fileMatchCount++;
    fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension));
}
else
    fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));