C#使用openFileDialog选择文件打开后,删除文件所在文件夹报错怎么解决? 50
我直接打开、删除操作没有问题(程序段B),可使用openFileDialog选择文件打开后,执行到Deldir.Delete(true);就提示“文件“D:\testfi...
我直接打开、删除操作没有问题(程序段B),可使用openFileDialog选择文件打开后,执行到Deldir.Delete(true);就提示“文件“D:\testfile\testdir”正由另一进程使用,因此该进程无法访问文件”。
以下程序是程序代码:
private void button1_Click(object sender, EventArgs e) { string LPath = ""; string LDir = ""; string[] SpStr; FileStream fs; ///程序段A///OPENFILEDIALOG打开后删除ERROR openFileDialog1.FileName = ""; openFileDialog1.Filter = "数据文件(*.txt)|*.txt"; openFileDialog1.FilterIndex = 1; openFileDialog1.ShowDialog(); LPath = openFileDialog1.FileName; SpStr = LPath.Split('\\'); for (int i = 0; i < SpStr.Length - 1; i++) { LDir = LDir + SpStr[i] + "\\"; } fs = new FileStream(LPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs.Close(); fs.Dispose(); DirectoryInfo Deldir = new DirectoryInfo(LDir); Deldir.Delete(true); ///程序段A///
///程序段B///直接删除测试OK //fs = new FileStream("D:\\testfile\\testdir\\t1.txt", FileMode.Open, FileAccess.Read); //LDir = "D:\\testfile\\testdir\\";
//fs.Dispose();
//DirectoryInfo Deldir = new DirectoryInfo(LDir); //Deldir.Delete(true); ///程序段B/// } 展开
以下程序是程序代码:
private void button1_Click(object sender, EventArgs e) { string LPath = ""; string LDir = ""; string[] SpStr; FileStream fs; ///程序段A///OPENFILEDIALOG打开后删除ERROR openFileDialog1.FileName = ""; openFileDialog1.Filter = "数据文件(*.txt)|*.txt"; openFileDialog1.FilterIndex = 1; openFileDialog1.ShowDialog(); LPath = openFileDialog1.FileName; SpStr = LPath.Split('\\'); for (int i = 0; i < SpStr.Length - 1; i++) { LDir = LDir + SpStr[i] + "\\"; } fs = new FileStream(LPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs.Close(); fs.Dispose(); DirectoryInfo Deldir = new DirectoryInfo(LDir); Deldir.Delete(true); ///程序段A///
///程序段B///直接删除测试OK //fs = new FileStream("D:\\testfile\\testdir\\t1.txt", FileMode.Open, FileAccess.Read); //LDir = "D:\\testfile\\testdir\\";
//fs.Dispose();
//DirectoryInfo Deldir = new DirectoryInfo(LDir); //Deldir.Delete(true); ///程序段B/// } 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询