文件删除,移动,复制

1:删除
  
    
try
{
// 指定路径和文件名
string filePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath( " ~/upload " ), " b.doc " );
string query = " 确定要删除该文件\n " + filePath + " ? " ;
if (MessageBox.Show(query, " 删除文件? " , MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Delete(filePath);
}
}
catch (Exception ex)
{
MessageBox.Show(
" 无法删除该文件,以下异常! " + " 发生:\n " + ex.Message, " 失败 " );
}

2:移动

  
    
try
{
// 指定路径和文件名
string filePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath( " ~/upload " ), " b.doc " );
string query = " 确定要移动该文件\n " + filePath + " ? " ;
if (MessageBox.Show(query, " 移动文件? " , MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Move(filePath, System.Web.Hosting.HostingEnvironment.MapPath(
" ~/Newupload/ " ) + " NewfileName.doc " );
}
}
catch (Exception ex)
{
MessageBox.Show(
" 无法移动该文件,以下异常! " + " 发生:\n " + ex.Message, " 失败 " );
}

3:复制

  
    
try
{
// 指定路径和文件名
string filePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath( " ~/upload " ), " b.doc " );
string query = " 确定要复制该文件\n " + filePath + " ? " ;
if (MessageBox.Show(query, " 复制文件? " , MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Copy(filePath, System.Web.Hosting.HostingEnvironment.MapPath(
" ~/Newupload/ " ) + " NewfileName.doc " );
}
}
catch (Exception ex)
{
MessageBox.Show(
" 无法复制该文件,以下异常! " + " 发生:\n " + ex.Message, " 失败 " );
}

你可能感兴趣的:(文件)