C#能不能实现保存图片时,若该文件夹中有同名图片,如原有图片名为“图片.jpg”,保存时自动变为“图片1.jpg”?若可以,要怎么写?

解决方案 »

  1.   

    Guid.NewGuid().ToString();绝对不会有重名的
      

  2.   

    使用File.exist(string filename) 查找目录下是否有这个文件 如果有 那么你save文件的时候 把文件名后面(不包括扩展名) 后面加上一个 Math.random();
      

  3.   


    public static string GetUniqueFileName(string fullName)
    {
    // 如果不存在fullName指定的文件,直接返回它。
    if (!System.IO.File.Exists(fullName)) return fullName; string dir = System.IO.Path.GetDirectoryName(fullName);
    string name = System.IO.Path.GetFileNameWithoutExtension(fullName);
    string ext = System.IO.Path.GetExtension(fullName);
    string unique;
    int index = 1;

    do {
    unique = System.IO.Path.Combine(dir, string.Format("{0}[{1}].{2}", name, index++, ext));
    } while (System.IO.File.Exists(unique)); return unique;
    }传入参数是你期望的文件全名,注意哦是全名。
    代码随手写的,仅供参考,不保证能够正常使用。
      

  4.   

    http://aspxuexi.blog.hexun.com/11238690_d.html
    去看下,不知道对你有没有帮助
      

  5.   

    可以的
    http://kb.cnblogs.com/a/1165129/代码是全的你看看