C#_C#实现保存文件时重名自动生成新文件的方法,本文实例讲述了C#实现保存文
将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).txt
/// <summary> /// Generates a new path for duplicate filenames. /// </summary> /// <param>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; }
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/c/9579.shtml
