Luiey
Luiey

Reputation: 873

Delete directory on network shared folder

This question is bring from another forum which not having answer yet for my situation.

I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:

  1. Check destination (network shared folder) path is empty or not.
  2. Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
  3. Copy folder content from source to new destination.

No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?

Delete directory code that I use but exception "Could not complete operation since directory is a root directory":

My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)

Please assist

EDITED:

To delete all files inside the main directory:-

Dim directory As New DirectoryInfo(strDestination)

For Each file As FileInfo In directory.GetFiles()
       file.Delete()
Next file

To delete all folders inside the main directory:-

For Each folder As DirectoryInfo In directory.GetDirectories()
       folder.Delete(True)
Next folder

Upvotes: 0

Views: 3154

Answers (1)

Dai
Dai

Reputation: 155280

Use this instead (it's C#, you'll need to convert it to VB.NET):

DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
    file.Delete();
}

Upvotes: 1

Related Questions