簡體   English   中英

訪問路徑被拒絕錯誤

[英]Access to Path Denied Error

我有一個需要刪除完整目錄的程序,但出現Access to path denied錯誤。 我對所有人都具有完全訪問權限,但並沒有幫助。

private void timer1_Tick(object sender, EventArgs e)
{
    label1.Text = DateTime.Now.ToString("HH:mm");
    string lowDir = "";
    if (label1.Text == comboBox1.Text)
    {
        try
        {
            if (System.IO.Directory.GetDirectories(txpath1.Text).Length != 0)
            {
                try
                {
                    var folders = Directory.GetDirectories(@"" + txpath1.Text);
                    DateTime lastLow = DateTime.Now;

                    foreach (string subdir in Directory.GetDirectories(@"" + txpath1.Text))
                    {
                        DirectoryInfo fi1 = new DirectoryInfo(subdir);
                        fi1.Refresh();
                        DateTime created = fi1.LastWriteTime;
                        if (created < lastLow)
                        {
                            lowDir = subdir;
                            lastLow = created;
                        }
                    }
                    var dir = new DirectoryInfo(@"" + lowDir);
                    Directory.Delete(@"" + lowDir, true);
                }
                catch (Exception ex)
                {
                    this.btStop.PerformClick();
                    MessageBox.Show(ex.Message+"\nEzért a program leállt.");
                }
                finally
                {
                    timer1.Stop();
                    timer2.Start();
                }
            }
            else
            {
                this.btStop.PerformClick();
                MessageBox.Show("A megadott elérési útvonalon nem találhatóak fájlok a művelet(ek)hez!");
            }
        }
        catch (DirectoryNotFoundException)
        {
            btStop.PerformClick();
            MessageBox.Show("Hibás útvonala(kat)t adtál meg!");
        }
    }
}

您只需要使用Directory.Delete(path, true)來遞歸刪除。 如果這不起作用,則可能是路徑格式錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM