Delete directory on network shared folder - vb.net

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:
Check destination (network shared folder) path is empty or not.
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.
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

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();
}

Related

Showing the File Extensions inside the folder

Currently, I am working on a feature that will make the files inside the folder that will not hide the file extensions using this code.
Imports Microsoft.Win32
Sub SetNoDrives(value As Integer, path as string)
Dim RegPath As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]"
Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey(RegPath)
Key.SetValue("HideFileExt", value, RegistryValueKind.DWord)
End Using
End Sub
the problem is, I don't know where to place the string path (folder path) on the code. the path is the specific location where you will always show the file extensions of the files inside the folder.
Any help will be much appreciated.
The reason you don't know where to put the folder path is because there is nowhere to put the folder path. This is a user-wide option, i.e. either extensions are displayed in every folder or no folder for the current user. You don't get to choose on a folder by folder basis. At least, there is no option in Windows/File Explorer to do that and I've never seen mention of it being possible, even when specifically searching for it.

File not copy to correct directory

I am trying to copy a file from a network drive then move to the different folder. Somehow is not moving the file to the directory I specified. After executing the read file code, the file is copy in the same directory.(see pic)
I have try the same code from my local drive and it works fine.
'copy the file
File.Copy(Path.Combine("\\swnas.swmed.org\Phytel\", Dts.Variables("File").Value.ToString), Path.Combine("\\swnas.swmed.org\Phytel\Raw\MCKPP\Processed", insertNewName), True)
'read the file
objStreamReader = New StreamReader(Path.Combine("\\swnas.swmed.org\Phytel\Raw\MCKPP\Processed", insertNewName))
'overwrite the file
objWriter = New StreamWriter(Dts.Variables("File").Value.ToString, False)
I'd take a minute and make sure there are no odd permission rules being applied to the directory you are copying/moving to.

VB.NET Iteration and folder operations

I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file
Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.
For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.

Making folder and copying file

I want my application to look for a file in drive C's specific folder (say C:\myFolder\abc.mdb), if found just give a message if not, make the folder in drive C:\ and then copy the file.
How to do this?
Thanks
Furqan
You could use the File, Directory, and Path objects in the System.IO as shown below:
Imports System.IO
...
Dim path As String = "C:\myFolder\abc.mdb"
If File.Exists(path) Then
'TODO write code to create message'
Else
Dim folder As String = Path.GetDirectoryName(path)
If Not Directory.Exists(folder) then
Directory.CreateDirectory(folder)
End If
'TODO code to copy file from current location to the newly created directory path'
'i.e. File.Copy(FileToCopy, NewCopy)'
End If
I know this post is kind of old. I just wanted to update.
The quickest shortcut that will also create the Destination directory structure and in one line. Use Computer.FileSystem.CopyFile instead of System.IO.
My.Computer.FileSystem.CopyFile(sSourcefile, sDestinationfile)

Protect single file!

I'm trying to protect a folder and the files inside it.
I'm able to protect the folder itself, so that if somebody clicks on it he will get a message:
"You don't currently have permission to access this folder!"
But I can still access files in that folder. For example, if somebody knows the name of a file inside the folder he can type D:\ProtectedFolder\pdffile.pdf and he can open the file!
So, my question is:
Can I protect single files inside the folder?
This is the function that I use for folder lock:
Public Function Lock(ByVal folder As
String, ByVal user As String)
Dim FilePath As String = folder
Dim fs As FileSystemSecurity = File.GetAccessControl(FilePath)
fs.AddAccessRule(New FileSystemAccessRule(user,
FileSystemRights.ListDirectory,
AccessControlType.Deny))
fs.AddAccessRule(New FileSystemAccessRule(user,
FileSystemRights.FullControl,
AccessControlType.Deny))
File.SetAccessControl(FilePath, fs)
Return 0
End Function
Thanks!
You will also have to deny FileSystemRights.Read if you want to prevent that. And technically you have to make sure that the files inherited their rights from the folder.
Specify FileShare.None for File.Open. You can see my C# implementation of it here with full source code. Convert it to VB.NET if you'd like.
This is the message you receive when trying to open a file locked by the application:
I think that's what you're after.