2017-06-19 60 views
1

我想建立一个Web应用程序,将允许最终用户在选择之前,选择一个特定的日期和文件类型,并删除所有类型的文件日期在选定的NAS上。问题是,一些NAS的,可以通过我的代码试图枚举所有这些文件,我的网页将已超时的时间有多达39TB数据值得其中。如何通过大量的文件(1个磨+)的effeciently递归

我曾尝试使用一个后台工作的帮助,但是这似乎没有什么区别。如果有人可以看看下面的代码,也许可以指出一个更好的方向。

<div class="PanelColour"> 
     <div class="auto-style3"> 
      <img src="Images/MainLogo.jpg" class="auto-style3" /> 
     </div> 

     <div> 
      <span class="heading">Select a Nas </span> 
      <div> 
      </div> 
      <asp:DropDownList ID="DropDownList1" runat="server" CssClass="MainText" Width="230px" AutoPostBack="True"> 
       <asp:ListItem Text="dubfilm01-nas" Value="1"></asp:ListItem> 
       <asp:ListItem Text="dubfilm02-nas" Value="2"></asp:ListItem> 
       <asp:ListItem Text="dubfilm03-nas" Value="3"></asp:ListItem> 
       <asp:ListItem Text="dubfilm04-nas" Value="4"></asp:ListItem> 
       <asp:ListItem Text="dubfilm05-nas" Value="5"></asp:ListItem> 
       <asp:ListItem Text="dubfilm06-nas" Value="6"></asp:ListItem> 
       <asp:ListItem Text="dubfilm07-nas" Value="7"></asp:ListItem> 
       <asp:ListItem Text="dubfilm08-nas" Value="8"></asp:ListItem> 
       <asp:ListItem Text="dubfilm09-nas" Value="9"></asp:ListItem> 
       <asp:ListItem Text="dubfilm10-nas" Value="10"></asp:ListItem> 
       <asp:ListItem Text="dubfilm11-nas" Value="11"></asp:ListItem> 
       <asp:ListItem Text="dubfilm12-nas" Value="12"></asp:ListItem> 
       <asp:ListItem Text="dubfilm13-nas" Value="13"></asp:ListItem> 
       <asp:ListItem Text="dubfilm14-nas" Value="14"></asp:ListItem> 
       <asp:ListItem Text="dubfilm15-nas" Value="15"></asp:ListItem> 
       <asp:ListItem Text="dubfilm16-nas" Value="16"></asp:ListItem> 
       <asp:ListItem Text="dubfilm17-nas" Value="17"></asp:ListItem> 
       <asp:ListItem Text="dubfilm18-nas" Value="18"></asp:ListItem> 
       <asp:ListItem Text="dubfilm19-nas" Value="19"></asp:ListItem> 
       <asp:ListItem Text="dubfilm20-nas2" Value="20"></asp:ListItem> 
       <asp:ListItem Text="dubfilm21-nas2" Value="21"></asp:ListItem> 
       <asp:ListItem Text="dubfilm22-nas2" Value="22"></asp:ListItem> 
       <asp:ListItem Text="dubfilm23-nas2" Value="23"></asp:ListItem> 
       <asp:ListItem Text="dubfilm24-nas" Value="24"></asp:ListItem> 
       <asp:ListItem Text="dubfilm25-nas" Value="25"></asp:ListItem> 
       <asp:ListItem Text="dubfilm26-nas" Value="26"></asp:ListItem> 
       <asp:ListItem Text="Select a Nas" Value="27" Selected="True"></asp:ListItem> 



      </asp:DropDownList> 

      <div class="auto-style4"> 
       <table class="auto-style6"> 
        <tr class="subheading"> 
         <th class="auto-style5">Select files to delete</th> 
         <th class="auto-style7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Select date to delete from</th> 

        </tr> 
        <tr> 
         <td class="auto-style5"> 
          <asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="203px" Height="184px"> 
           <asp:ListItem Text=" Image files " Value="jpeg" class="MainText"></asp:ListItem> 
           <asp:ListItem Text=" Text files " Value="jpeg" class="MainText"></asp:ListItem> 
           <asp:ListItem Text=" All files " Value="jpeg" class="MainText"></asp:ListItem> 


          </asp:CheckBoxList> 
         </td> 
         <td class="auto-style7"> 

          <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" ForeColor="Black" Height="196px" Width="488px" CssClass="MainText" NextPrevFormat="FullMonth"> 
           <DayHeaderStyle Font-Bold="True" Font-Size="11pt" /> 
           <NextPrevStyle Font-Size="8pt" ForeColor="#333333" Font-Bold="True" VerticalAlign="Bottom" /> 
           <OtherMonthDayStyle ForeColor="#999999" /> 
           <SelectedDayStyle BackColor="#333399" ForeColor="White" /> 
           <TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px" Font-Bold="True" Font-Size="12pt" ForeColor="#333399" /> 
           <TodayDayStyle BackColor="#CCCCCC" /> 
          </asp:Calendar> 

         </td> 
         <td class="auto-style8"> 
          <asp:Button ID="Button2" runat="server" Text="Delete " CssClass="buttonBL" OnClick="Button2_Click" Width="98px" /> 
         </td> 


        </tr> 
       </table> 
      </div> 












      <div> 
      </div> 
      <br /> 
      <div> 



       <asp:Button ID="Button1" runat="server" Text="Navigate to Folder" Class="buttonBL" Height="57px" OnClick="Button1_Click" Width="161px" /> 
       &nbsp&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 


      <asp:TextBox ID="TextBox1" runat="server" Height="51px" Width="787px" CssClass="MainText"></asp:TextBox> 


      </div> 



      </tr> 




      <br /> 





     </div> 
</form> 

和背后的C#代码看起来像这样

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Trinet.Networking; 

public partial class Admin : System.Web.UI.Page 
{ 
    private static BackgroundWorker worker = new BackgroundWorker(); 
    private event EventHandler BackgroundWorkFinished; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      worker.DoWork += worker_DoWork; 
      worker.RunWorkerCompleted += worker_RunWorkerCompleted; 
      worker.ProgressChanged += worker_ProgressChanged; 
      worker.WorkerReportsProgress = true; 
      worker.WorkerSupportsCancellation = true; 
     } 
    } 
protected void Button2_Click(object sender, EventArgs e) 
{ 
    DateTime d2 = Calendar1.SelectedDate; 
    string nas = DropDownList1.SelectedItem.Text; 
    object[] args = { d2, nas }; 
    if (worker.IsBusy != true) 
    { 
     worker.RunWorkerAsync(args); 
    } 

} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 

     System.Diagnostics.Process process = new System.Diagnostics.Process(); 
     System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
     startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     startInfo.FileName = "cmd.exe"; 
     string _path = "\\\\" + DropDownList1.SelectedItem.Text; 
     startInfo.Arguments = string.Format("/C start {0}", _path); 
     process.StartInfo = startInfo; 
     process.Start(); 
    } 

    catch 
    { 

    } 

} 
private int Getfilesdate(DateTime d2) 
{ 
    try 
    { 
     DateTime d1 = DateTime.Now; 
     // DateTime d2 = Calendar1.SelectedDate; 
     TimeSpan t = d1 - d2; 
     int NrOfDays = Convert.ToInt16(t.TotalDays); 
     return (NrOfDays); 
    } 
    catch 
    { 
     return 0; 
    } 
} 
private void getfiles(int days,string path) 
{ 
    uncpath(path, days); 
} 
private string uncpath(string server, int days) 

{ 

    string nas = server; 
    string SI = ""; 
    ShareCollection shi = ShareCollection.LocalShares; 
    if (server != null && server.Trim().Length > 0) 
    { 
     shi = ShareCollection.GetShares(server); 
     if (shi != null) 
     { 
      foreach (Share si in shi) 
      { 
       deletefiles(si.ToString(), days); 

      } 
      return ""; 
     } 
    } 

    return ""; 
} 
private void deletefiles(string path, int days) 
{ 
    try 
    { 

     // int nofiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) 
     // .Where(s => s.ToLower().EndsWith(".tiff") || s.ToLower().EndsWith(".jpg")).Count(); 

     TextBox1.Text = string.Empty; 
    // TextBox1.Text = "There are "+nofiles + " in all directories"; 
     int i = 1; 
     worker.ReportProgress(i); 

     var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) 
     .Where(s => s.ToLower().EndsWith(".tiff") || s.ToLower().EndsWith(".jpg")); 

     foreach (string file in files) 
     { 
      FileInfo info = new FileInfo(file); 
      if (info.LastWriteTime < DateTime.Now.AddDays(-days)) 
      { 

       File.Delete(file); 

      } 
     } 

    } 
    catch 
    { 

    } 
} 

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 

    int i = Convert.ToInt16(e.ProgressPercentage); 
    if (i == 0) 
     { 
     TextBox1.Text = "Gathering file information................."; 
     } 
    else 
    { 

     TextBox1.Text = "..............This may take a while, grab a cup of coffee"; 
    } 
} 

private void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    Object[] arg = e.Argument as Object[]; 
    string path = (string)arg[1]; 
    DateTime d2 = (DateTime)Convert.ToDateTime(arg[0]); 
    int i = 0; 
    worker.ReportProgress(i); 
    int NrofDays = Getfilesdate(d2); 
    getfiles(NrofDays,path); 
} 

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    TextBox1.Text = string.Empty; 
    TextBox1.Text = "Completed"; 
} 
} 

我真的很感激一些帮助

感谢

回答

2

枚举FIL es是一种相对较慢的操作,在网络上更是如此。

我会保持在一个数据库中的文件信息的副本您安排定期更新,例如在Windows服务或迟发型工作。

然后你就可以显示从数据库中的文件信息,与索引(更快的搜索)和分页(更快的响应时间,为您的网页)的额外奖金。

+0

我想到了一个数据库的做法,但在一分钟有超过20 NAS与数据,从5TB到30 + TB所以这将是很多文件来存储,也有可能是一天保持跟踪100万个新文件等等。我想要做的是选择nas和日期范围并删除所有必要的文件。目前我只能手动通过nas执行此操作,而且非常耗时 – Engodel