locked
ILLEGAL CHARACTER IN PATH RRS feed

  • Question

  • User-786564416 posted

    I am trying to delete all the files in a directory as following:

     Dim AttachmentFileLocalPath As String
    
     If MurasalatTempDirectoryPath = "" Then 
         MurasalatTempDirectoryPath = GetMurasalatFileSystemPath()
     End If
    
    
     AttachmentFileLocalPath = MurasalatTempDirectoryPath & UserLocalFolder & "\" & "*.*"
     System.IO.File.Delete(AttachmentFileLocalPath)

    The AttachmentFileLocalPath value which we need to clear all its contents is: D:\E\TrackingDesk\TempDirectory\3KKYCS0GWHH6GWR636OGGT058IZOC2JA0BDBAMNP\*.*

    Where is the error in that? 

    Friday, February 8, 2019 10:46 PM

All replies

  • User409696431 posted

    System.IO.File.Delete deletes one file, and you pass it that file.  Wildcard characters are not supported.  There is no legal file name that ends in "*.*".

    If you are trying to delete all the files in a directory, iterate over the files in the directory and delete each one.  One way to do it (replacing the directory with your own):

    string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
    foreach (string filePath in filePaths)
      File.Delete(filePath);

    Friday, February 8, 2019 11:32 PM
  • User-1174608757 posted

    Hi alihusain_77

    According to your description, if there are not many files in you folder you could use .GetFiles() or .GetDirectories() to get each file in the folder then delete each one. You could  write as below:

    System.IO.DirectoryInfo di = new DirectoryInfo("FolderPath");
    
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete(); 
    }
    
    System.IO.DirectoryInfo di = new DirectoryInfo("FolderPath"); 
    foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); }

    However, when there are large amounts of files in your folder,I suggest you to use EnumerateFiles() which  is more efficient than GetFiles(). You could write as below:

    System.IO.DirectoryInfo di = new DirectoryInfo("FolderPath");
    foreach (FileInfo file in di.EnumerateFiles()) { file.Delete(); }
    System.IO.DirectoryInfo di = new DirectoryInfo("FolderPath");
    foreach (DirectoryInfo dir in di.EnumerateDirectories()) { dir.Delete(true); }

    Best Regards

    Wei Zhang 

    Monday, February 11, 2019 6:39 AM