删除文件夹
C# 一个压缩包里存放的是一些Excel文件,将其解压后,生成一个临时文件夹,Excel数据导入到数据库中,最后删除这个解压后的文件夹,提示文件**.xls正在被另一个进程所使用,请问如何处理 --------------------编程问答-------------------- 导入后,关闭excel操作,释放资源。 --------------------编程问答--------------------Process[] ps = System.Diagnostics.Process.GetProcessesByName("excel");
if (ps.Length > 0)
{
for(Int32 i=0;i<ps.Length;i++)
{
ps[i].Kill();
}
}
--------------------编程问答--------------------
+1 --------------------编程问答-------------------- 你要关闭 application Excel 啊,要不 内存锁住了,你不可能删除文件夹。
删除 文件夹 要 吧锁解除了
然后在
dir.Delete(True)
--------------------编程问答-------------------- 2楼的,请问代码写到哪里
--------------------编程问答-------------------- 请看一下我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Biz.Common;
using System.IO;
using accimg.common;
using System.Data.SqlClient;
using System.Diagnostics;
namespace Import
{
public partial class ImportExcel
{
ProgressBar bar;
Label label;
DataBackUpServer PZServer = new DataBackUpServer();
string connection = "";
public ImportExcel()
{
}
public ImportExcel(System.Windows.Forms.ProgressBar pb, System.Windows.Forms.Label lbl)
{
bar = pb;
label = lbl;
}
public void Import(string dataName, string Table, bool i)
{
string strPath = Environment.CurrentDirectory + "\\decompression\\Subjoin\\" + dataName + "\\";
DirectoryInfo di = new DirectoryInfo(strPath);
if (!di.Exists)
{
di.Create();
}
string strPath1 = Environment.CurrentDirectory + "\\decompression\\Accimg\\Accimg\\";
DirectoryInfo di1 = new DirectoryInfo(strPath1);
if (!di1.Exists)
{
di1.Create();
}
string connection = SqlHelper.connStr;
string connString = string.Format("{0}", connection.Substring(0, connection.IndexOf("database=")) + "database= " + dataName + connection.Substring(connection.IndexOf(";uid"), connection.Length - connection.IndexOf(";uid")));
if (i)
{
TransferData(Environment.CurrentDirectory + @"\\decompression\Subjoin\" + dataName + "\\" + Table + ".xls", Table, connString);
}
else
{
TransferData(Environment.CurrentDirectory + @"\\decompression\Accimg\Accimg\" + Table + ".xls", Table, connString);
}
}
public void TransferData(string excelFile, string sheetName, string connectionString)
{
DataSet ds = new DataSet();
try
{
//获取全部数据
string strConn = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + excelFile + ";" + "Extended Properties = 'Excel 8.0;HDR=YES,IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$] ", sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, sheetName);
//用bcp导入数据
//excel文件中列的顺序必须和数据表的列顺序一致,因为数据导入时,是从excel文件的第二行数据开始,不管数据表的结构是什么样的,反正就是第一列的数据会插入到数据表的第一列字段中,第二列的数据插入到数据表的第二列字段中,以此类推,它本身不会去判断要插入的数据是对应数据表中哪一个字段的
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
//bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//每次传输的行数
bcp.NotifyAfter = 100;//进度提示的行数
bcp.DestinationTableName = sheetName;//目标表
bcp.WriteToServer(ds.Tables[0]);
bar.Value += bar.Step;//让进度条增加一次
double value = Convert.ToDouble(Convert.ToInt16((Convert.ToDouble(bar.Value) / Convert.ToDouble(bar.Maximum)) * 1000)) / 10;
label.Text = value.ToString() + "%\r\n" + "当前正在执行将Excel数据读到数据库中";
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
public void ReadExcel(string excelFile, string sheetName)
{
try
{
DataSet ds = new DataSet();
//获取全部数据
string strConn = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Environment.CurrentDirectory + @"\decompression\pz\" + excelFile + "\\" + sheetName + ".xls" + ";" + "Extended Properties = 'Excel 8.0;HDR=YES,IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$] ", sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, sheetName);
//System.Data.SqlClient.SqlConnection
foreach (DataTable item in ds.Tables)
{
foreach (DataRow row in item.Rows)
{
string id = item.Columns[0].ColumnName;
SqlConnection sqlconnet = new SqlConnection(SqlHelper.connStr);
PZServer.DeleteID(Convert.ToInt16(row[id]), sheetName, "DaLian_2012", id, sqlconnet);
bar.Value += bar.Step;//让进度条增加一次
double value = Convert.ToDouble(Convert.ToInt16((Convert.ToDouble(bar.Value) / Convert.ToDouble(bar.Maximum)) * 1000)) / 10;
label.Text = value.ToString() + "%\r\n" + "当前正在执行将Excel数据读到数据库中";
}
}
}
catch (Exception )
{
throw;
}
}
}
} --------------------编程问答-------------------- 这是执行导入数据库的操作,调用这个方法后,在调用删除文件的方法。 --------------------编程问答--------------------
using (OleDbConnection conn = new OleDbConnection(strConn))
{
}
conn 要关闭
--------------------编程问答--------------------
public void DeleteFolder(string deleteDirectory)
{
try
{
if (Directory.Exists(deleteDirectory))
{
foreach (string deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
{
if (File.Exists(deleteFile))
{
File.Delete(deleteFile);
}
else
{
DeleteFolder(deleteFile);
}
}
Directory.Delete(deleteDirectory);
}
}
catch (Exception)
{
throw;
}
}
补充:.NET技术 , C#