2015-08-27 77 views
-3

它調試時發出此錯誤.Button1是關於zip文件,然後將其從源複製到目標..在刪除功能它刪除創建時間比給定的時間更早的文件.. 注:我很新,所以請儘可能在答案中提供大量細節。該路徑不是合法的形式

private void button1_Click(object sender, EventArgs e) 
{ 
    Backup._MAIN takeBackUp = new Backup._MAIN(); 
    try 
    { 
     takeBackUp.copyFile(textBox1.Text, textBox2.Text); 
     MessageBox.Show("done"); //do stuff 
    } 

    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    bool dater; 
    string err = ""; 
    Backup._MAIN formDelete = new Backup._MAIN(); 
    DateTime dtp = dateTimePicker1.Value; 
    dater = formDelete.deleteFiles(textBox3.Text, dtp,out err); 
    if (err.Length > 0) 
     MessageBox.Show(err); 
    else 
     MessageBox.Show("Done!"); 
} 

DLL:

public void copyFile(string sSource,string sDestination) 
{ 
    string sourcePath = sSource; 
    string targetPath = sDestination; 
    Directory.GetFiles(sourcePath,"",SearchOption.AllDirectories); 

    string myBackUp=""; 
    string fileName; 

    if (!System.IO.Directory.Exists(targetPath)) 
    { 
     System.IO.Directory.CreateDirectory(targetPath); 
    } 

    using (ZipFile zip = new ZipFile(targetPath)) 
    { 
     zip.AddDirectory(sourcePath, ""); 
     zip.Save(myBackUp); 
    } 

    if (System.IO.Directory.Exists(sourcePath)) 
    { 
     string[] fileList = System.IO.Directory.GetFiles(sourcePath, "*.rar*"); 

     foreach (string files in fileList) 
     { 
      // from the path. 
      fileName = System.IO.Path.GetFileName(files); 
      targetPath = System.IO.Path.Combine(targetPath, fileName); 
      System.IO.File.Copy(files, targetPath, true); 
     } 
    } 
} 

public bool deleteFiles(string sSource, DateTime dOlder, out string sError) 
{ 
    string path = sSource; 
    sError = ""; 
    try 
    { 
     string[] fileList = Directory.GetFiles(path); 
     foreach (string fname in fileList) 
     { 

      if (File.GetCreationTime(path + fname) <= dOlder) 
      { 
       File.Delete(path + fname); 
      } 
     } 
     return true; 
    } 

    catch (Exception ex) 
    { 
     sError = ex.ToString(); 
     return false; 
    } 
} 
+2

_IT把乳液在籃下也得到軟管again_你能改寫你的第一款,不知道你是什麼意思 – MickyD

+0

你能告訴我們真正的問題是什麼?你只是張貼代碼,並解釋它是什麼.. – Rob

+2

@MickyDuncan現在我覺得像一些蠶豆 –

回答

1

Directory.GetFiles()返回完整的路徑名。

然而,考慮你的代碼:

string[] fileList = Directory.GetFiles(path); 

foreach (string fname in fileList) 
{ 
    if (File.GetCreationTime(path + fname) <= dOlder) 

當你這樣做path + fname你前綴的路徑,這是會導致一個無效的路徑,因爲fname已經包含的路徑。

如果路徑中包含一個驅動器號(如C:\),那麼您最終會得到一個類似C:\MyDir\C:\MyDir\MyFile.txt的路徑,它會給出您看到的錯誤。

您需要做的僅僅File.GetCreationTime(fname)(在File.Delete()相同。

+0

是的,它的工作!非常感謝。你可以在copyFile函數中找到問題嗎? – GodLike

相關問題