private void bt_ModifyName_Click ( object sender , EventArgs e )
{
OpenFileDialog dlg = new OpenFileDialog ( );
dlg.AddExtension = false;
dlg.Filter = "所有文件(*.*)|*.*";
if ( dlg.ShowDialog ( ) == DialogResult.OK )
{
try
{
if ( cb_ModifyType.Checked )//单个修改
{
ModifyFileName ( dlg.FileName );
}
else //修改所有文件
{
FileInfo fInfo=new FileInfo ( dlg.FileName );
string[] filename=Directory.GetFiles ( fInfo.Directory.ToString ( ) );
string fileCount = filename.Length.ToString ( );
if ( MessageBox.Show ( "一定要修改\r[ " +fileCount+" ] 个文件的名字吗?", "修改提示" , MessageBoxButtons.OKCancel , MessageBoxIcon.Warning ) == DialogResult.OK )
水乡乌镇>周华健的组合叫什么{
foreach ( string sFile in filename )
{
ModifyFileName ( sFile );
}
}
}
}
catch ( Exception ex ) { WriteExceptionInfo ( ex ); }
}
}
/// <summary>
正月初九是黄道吉日吗2023/// 通过路径文件名
/// </summary>
public string TakeFileName ( string filePath )
{
int iTmp =0;
string sFile  ="";
iTmp = filePath.LastIndexOf ( '\\' );
if ( filePath.IndexOf ( ':' ) != 1 ) //如果传入的是路径,第2个字符一定要是“:”
return sFile;
if ( filePath.IndexOf ( '\\' ) != 2 ) //如果传入的是路径,第3个字符一定要是“\”
return sFile;
sFile = filePath.Substring ( iTmp + 1 );
iTmp = sFile.LastIndexOf ( '.' );
sFile = sFile.Substring ( 0 , iTmp );
return sFile;
}
/// <summary>
/// 通过文件名 取后缀
/// </summary>
public string TakePostfix ( string fileName )
{
int iTmp =0;
string sFile  ="";
iTmp = fileName.LastIndexOf ( '.' );
if ( iTmp <= 0 ) return sFile;
sFile = fileName.Substring ( iTmp );//后缀
return sFile;
}
/// <summary>
/// 修改文件名 sFilePath=文件路径
/// </summary>
刘銮雄扶正甘比public void ModifyFileName (string sFilePath)
{
try
{
string sName = TakeFileName ( sFilePath );  //无后缀
string sPostfix = TakePostfix
( sFilePath );//后缀
string sNewFileName ="";//新文件名
if ( cb_Insert.Checked )//替换
{ sNewFileName = tb_FileName.Text.Trim ( ); }
else //插入
{
if ( ( int ) nud_InsertPlace.Value > sName.Length )
sNewFileName = sName + tb_FileName.Text.Trim ( );
else
乔洋图片{
如何提高执行力sNewFileName = sName.Insert ( ( int ) nud_InsertPlace.Value , tb_FileName.Text.Trim ( ) );
}
}
FileInfo fInfo=new FileInfo ( sFilePath );
string sFileName = fInfo.Directory + @"\" + sNewFileName + sPostfix;
System.IO.File.Move ( sFilePath , sFileName );
lb_Info.Items.Insert ( 0 , "操作成功!" );
}
catch ( Exception ex ) { WriteExceptionInfo ( ex ); }
}