Monthly Archives: June 2012

Copy files from a secured network share

If you want to copy files from a source folder to some other folder, then the following code will work just fine:

static void Main(string[] args)
{
const string sourcePath = @"D:\Temp\Source";
const string destinationPath = @"D:\Temp\Destination";
Copy(sourcePath, destinationPath);
}

public static void Copy(string sourceDir, string targetDir)
{
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
}

However, if the source folder is a secured network drive that requires user name and password:

const string sourcePath = @"\\NETWORKSHARE\Source";

…you get an exception:

ex

To make this work, we have to find a way to provide user name and password before we try to copy the files. To do this, we can make use of two unmanaged WIN32 API functions:

First, we are going to add the structures needed by these operations:

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
/// <summary>
/// The scope of the enumeration.
/// </summary>
public ResourceScope RecourceScope;

/// <summary>
/// The type of resource.
/// </summary>
public ResourceType ResourceType;

/// <summary>
/// The display options for the network object in a network

/// browsing user interface.
/// </summary>
public ResourceDisplaytype ResourceDisplaytype;

/// <summary>
/// A set of bit flags describing how the resource can be used.
/// </summary>
public Usage Usage;

/// <summary>
/// Specifies the name of a local device.
/// </summary>
public string LocalName;

/// <summary>
/// Specifies the remote network name.
/// </summary>
public string RemoteName;

/// <summary>
/// A comment supplied by the network provider.
/// </summary>
public string Comment;

/// <summary>
/// The name of the provider that owns the resource.
/// </summary>
public string Provider;
}

/// <summary>
/// The resource scopes.
/// </summary>
public enum ResourceScope : int
{
Connected = 0,
GlobalNetwork = 1,
Remembered = 2,
};

/// <summary>
/// The resource types.
/// </summary>
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2
}

/// <summary>
/// The resource display types.
/// </summary>
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}

/// <summary>
/// The usages.
/// </summary>
public enum Usage : int
{
Connectable = 0x01,
Container = 0x02,
NoLocalDevice = 0x04,
Sibling = 0x08,
Attached = 0x10,
}

Once these are defined, we can create a class that contains the logic to add the network connection and also closes it automatically when it gets disposed.

/// <summary>
/// Opens a connection to a network resource. Automatically closes
/// the connection when disposed.
/// </summary>
public class NetworkConnection : IDisposable
{
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name,
int flags, bool force);

bool _disposed = false;
readonly string _remoteName;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="remoteName">Name of the network resource.</param>
/// <param name="networkCredential">Credentials for connection.</param>
public NetworkConnection(string remoteName,
NetworkCredential networkCredential)
{
_remoteName = remoteName;

var netResource = new NetResource()
{
ResourceType = ResourceType.Disk,
RemoteName = remoteName
};

var result = WNetAddConnection2(
netResource,
networkCredential.Password,
networkCredential.UserName,
0);

if (result != 0)
{
throw new Win32Exception(result,
"Error connecting to network resource");
}
}

/// <summary>
/// Destructor.
/// </summary>
~NetworkConnection()
{
Dispose(false);
}

/// <summary>
/// Called when object needs to be disposed.
/// </summary>
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

/// <summary>
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code.
// Managed and unmanaged resources can be disposed.
// If disposing equals false, the method has been called by the runtime
// from inside the finalizer and you should not reference other objects.
// Only unmanaged resources can be disposed.
/// </summary>
/// <param name="disposing">True if called by user's code,
/// false if called by runtime.</param>
protected void Dispose(bool disposing)
{
if (!_disposed)
{
// If disposing equals true, dispose all managed and
// unmanaged resources.
if (disposing)
{
// Dispose managed resources.
}

// Call the appropriate methods to clean up unmanaged resources
// here. If disposing is false, only the following code is
// executed.
WNetCancelConnection2(_remoteName, 0, true);

_disposed = true;
}
}
}

And now we can use this class as follows:

static void Main(string[] args)
{
const string sourcePath = @"\\NETWORKSHARE\Source";";
const string destinationPath = @"
D:\Temp\Destination";
using (new NetworkConnection(@"
\\NETWORKSHARE ",
new NetworkCredential(@"
username", "password")))
{
Copy(sourcePath, destinationPath);
}
}

Advertisements

Transform collection to comma separated list

Today I had to transform a collection of integers into a string in the form of a comma separated list. In the old-style- approach it would probably be something like:

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
StringBuilder strb = new StringBuilder();
foreach (int number in numbers)
{
strb.Append(number.ToString());
strb.Append(",");
}
string commeSeparatedList = strb.ToString().TrimEnd(',');

But actually this can be done in a short way, like:

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
string commaSeparatedList = string.Join(",",
numbers.Select(n => n.ToString()).ToArray());

which produces the same result. In my case it was not a collection of integers, but a list of objects; but it’s the same approach:
 
string commaSeparatedListOfGroups = string.Join(",", 
allRelatedGroups.Select(g => g.Id.ToString()).ToArray());