Raskolnikov Raskolnikov - 1 month ago 11
C# Question

How to rename folder in network shared folder?

I need to rename folder on network (I am using VPN).
Since this is on network I have to connect using credentials. I have all permissions to read and write in shared folder.
Same logic with connection to network is working when I want to delete, edit or save in same folder.

Same function is working for rename files.

Problem is just when I want to rename Folder !

This is function:

public async Task<bool> Rename(string oldPath, string newPath )
{
using (var network = new NetworkConnection(configuration.Value.Host, networkCredential))
{
network.Connect();
File.Move(oldPath, newPath);
return await Task.FromResult(true);
}
}


For example this are function parameters:


oldPath => \\10.174.133.199\SharedFolder\MyFolder

newPath => \\10.174.133.199\SharedFolder\RenamedFolder

Answer

The issue you are facing is that you are using File.Move - a folder is not a File, but instead a Directory, as such you should use Directory.Move instead

Directory.Move(oldPath, newPath);
Comments