当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现保存文件时重名自动生成新文件的方法

C#实现保存文件时重名自动生成新文件的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#实现保存文件时重名自动生成新文件的方法。分享给大家供大家参考。具体如下: 将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).

本文实例讲述了c#实现保存文件时重名自动生成新文件的方法。分享给大家供大家参考。具体如下:

将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).txt

/// <summary>
/// generates a new path for duplicate filenames.
/// </summary>
/// <param name="path">the path.</param>
/// <returns></returns>
private string getnewpathfordupes( string path )
{
  string directory = path.getdirectoryname( path );
  string filename = path.getfilenamewithoutextension( path );
  string extension = path.getextension( path );
  int counter = 1;
  string newfullpath;
  do
  {
  string newfilename = "{0}({1}).{2}".formatwith( filename, counter, extension );
  newfullpath = path.combine( directory, newfilename );
  counter++;
  } while ( system.io.file.exists( newfullpath ) );
  return newfullpath;
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网