-4

I want to delete all files from the folder if the specified directory exist. I tried with below code, but it is giving me error as

Additional information: Could not find a part of the path 'D:\Username\VSATFINAL_353\VSATTSSRSurvey\NESSPATH\VSAT\SignedDoc\I-KA-CTPR-ENB-9016C3.pdf'.

I tried with below code but it is not working.

protected void delete_ServerClick(object sender, EventArgs e)
    {
        string strServerFolder = "";            
        string strSapID = EncryptDecrypt.Decrypt(HttpUtility.UrlDecode(Request.QueryString["SapId"]));
        string strCandidateId = EncryptDecrypt.Decrypt(HttpUtility.UrlDecode(Request.QueryString["CandidateId"]));


        strServerFolder = Server.MapPath(ConfigurationManager.AppSettings["VSATSharedPath"].ToString() + strSAPCandidate + @"\" + "SignedDoc" + @"\");

        string strFilePath = Server.MapPath(ConfigurationManager.AppSettings["VSATSharedPath"].ToString() + strSAPCandidate + @"\" + "SignedDoc" + @"\" + strSapID + "_" + strCandidateId + ".pdf" + @"\");

        if (!Directory.Exists(strServerFolder))
        {
            Directory.Delete(strFilePath);
        }
    }
Power Star
  • 1,724
  • 2
  • 13
  • 25
Nad
  • 4,605
  • 11
  • 71
  • 160
  • Looking at your code, you are trying to delete a file path using `Directory.Delete` which wont work. – Valeklosse Aug 03 '17 at 07:32
  • @Valeklosse: So how could I delete the file ? – Nad Aug 03 '17 at 07:33
  • Have you tried File.Delete(path)? And not sure if it's a typo, but as the code stands now you try to remove something if the directory does NOT exist – CuccoChaser Aug 03 '17 at 07:33
  • @VVVV `File.Delete` should do the trick, there is also a `File.Exists` method which is probably a good idea to use before deleting the file. – Valeklosse Aug 03 '17 at 07:34
  • @AssaultingCuccos: No, let me try that – Nad Aug 03 '17 at 07:34
  • @AssaultingCuccos: not working getting error as `Additional information: Could not find a part of the path 'D:\Username\VSATFINAL_353\VSATTSSRSurvey\NESSPATH\VSAT\I-DL-DLHI-ENB-3305C2\SignedDoc\'.` – Nad Aug 03 '17 at 07:36
  • @AssaultingCuccos: so what is the correct way of deleting the files ?? u can suggest me and I will change it accordingly – Nad Aug 03 '17 at 07:40
  • Using `Directory/File.Exists` does not mean you can skip handling the exception.. some one could delete the file between your check and your actual call to `.Delete`.. – Peter Aug 03 '17 at 07:40
  • @Peter: Kindly suggest me the standard way of deleting it. any link will also do – Nad Aug 03 '17 at 07:41
  • @VVVV Have you checked this post: https://stackoverflow.com/questions/12297024/how-to-delete-all-files-from-a-specific-folder? There are many ways to delete files but the directory path should be valid one to do it. – Tetsuya Yamamoto Aug 03 '17 at 07:41
  • @VVVV look at my answer below. – Peter Aug 03 '17 at 07:53
  • Something I tried: `if (Directory.Exists(strServerFolder) && Directory.EnumerateFiles(targetFolder, "*", SearchOption.AllDirectories).Any() == true) { Array.ForEach(Directory.GetFiles(targetFolder), File.Delete) }`. – Tetsuya Yamamoto Aug 03 '17 at 07:55
  • @TetsuyaYamamoto: yes its working but when I again upload the file, it doesn't uploads the file – Nad Aug 03 '17 at 09:10
  • Possible duplicate of [How to delete all files and folders in a directory?](https://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory) – VDWWD Aug 03 '17 at 09:13

3 Answers3

1

You are deleting a directory instead of a file:

    if (!Directory.Exists(strServerFolder))
    {
        Directory.Delete(strFilePath);
    }

try

    if (!File.Exists(strServerFolder))
    {
        File.Delete(strFilePath);
    }
Peter
  • 27,590
  • 8
  • 64
  • 84
  • thanks peter, one more thing here. I want to check if file other `strServerFolder` is uploaded then it should stop it. How to check that – Nad Aug 03 '17 at 11:55
1

The documentation contains all the exceptions that can be thrown https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx#Anchor_1

try
{
    File.Delete(strFilePath);
}
catch (DirectoryNotFoundException ex)
{
    //File not found
}
catch (IOException ex)
{
    //File in use
}
catch (UnauthorizedAccessException ex)
{
    //Access denied
}
Peter
  • 37,042
  • 39
  • 142
  • 198
  • sorry peter, went for meeting. I tried with your code. But getting error as `{"The filename, directory name, or volume label syntax is incorrect.\r\n"}` – Nad Aug 03 '17 at 08:58
  • i tried with `strServerFolder` but I am getting exception as `Access to the path 'D:\Username\VSATFINAL_353\VSATTSSRSurvey\NESSPATH\VSAT\I-TN-ARKM-ENB-0014_C1\SignedDoc' is denied.` – Nad Aug 03 '17 at 09:02
  • @VVVV Then that means that the user that is running the code does not have access to the specified file or directory. try running the code as a different user or give the current user access to the file/directory. – Peter Aug 04 '17 at 11:16
0

If you have to delete file, you should use File.Delete method. And, you should check if file exists, not directory. If directory doesn't exist, you will get false when checking file, and more importantly, directory might exist but file may still missing and you will not be able to delete it. So, you should have:

if (File.Exists(strFilePath))
{
    File.Delete(strFilePath);
}

Additionally, you are trying to delete if directory doesn't exist which is impossible by definition and will always throw exception when it happens that there is no directory (and if it is there, it will be ignored).

Shadowed
  • 956
  • 7
  • 19