.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Copy file in LAN network


The mechanism of copying files in a local network is the same when you query data from a database. We get 3 basic steps:

  • Map the network drive – as open connection
  • Copy file – as query data
  • Reject the above drive – as close connection

The first and the last steps are done with net.exe windows program while the second step works with the normal File.Copy functionality.

First step – Map the network

public int MapDrive()
{
	int ret = 0; //successful

	try
	{
		string argument=@"use \\192.168.100.10\SharedDir password /user:192.168.100.10\user"

		NetResult netResult = NetUse(argument);

		if (!string.IsNullOrEmpty(netResult.ErrorOutput))
		{
			if (netResult.ErrorOutput.Contains("53"))
			{
				ret = 1;//network drive error (wrong ip address or 'SharedDir' is not exist)
			}
			else
			{
				ret = 2;//authentication error (wrong user name or password)
			}
			
		}
	}
	catch (Exception e)
	{
		ret = 2;//authentication error (wrong user name or password)
		Console.WriteLine("Exception when executing authentication with server");
		Console.WriteLine(e);
	}

	return ret;
}

Second step – Copy file
This step you can copy file as usual, using File.Copy supported by C#
Last step – Reject drive

public void RejectDrive()
{
	try
	{
		string argument = "use \\192.168.100.10\SharedDir /delete"

		NetResult netResult = NetUse(argument);

	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}

In the above first and last steps, i use the function NetUse which is defined as below:

private NetResult NetUse(string argument)
{
	var result = new NetResult();
	try
	{
		var startInfo = new ProcessStartInfo("net.exe", argument)
		{
			WindowStyle = ProcessWindowStyle.Hidden,
			UseShellExecute = false,
			RedirectStandardOutput = true,
			RedirectStandardError = true,
			CreateNoWindow = true
		};
		string successOutput = "";
		string errorOutPut = "";
		var process = Process.Start(startInfo);

		while (!process.StandardOutput.EndOfStream || !process.StandardError.EndOfStream)
		{
			try
			{
				successOutput += process.StandardOutput.ReadLine();
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
			try
			{
				errorOutPut += process.StandardError.ReadLine();
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		result.SuccessOutput = successOutput;
		result.ErrorOutput = errorOutPut;
	}
	catch
	{

	}

	return result;
}

Here, i defined the object NetResult to get out the result of the command net use

class NetResult
{
	/// <summary>
	/// Gets or sets the success output.
	/// </summary>
	/// <value>
	/// The success output.
	/// </value>
	public string SuccessOutput { get; set; }

	/// <summary>
	/// Gets or sets the error output.
	/// </summary>
	/// <value>
	/// The error output.
	/// </value>
	public string ErrorOutput { get; set; }

	/// <summary>
	/// Initializes a new instance of the <see cref="NetResult"/> class.
	/// </summary>
	/// <param name="successOutput">The success output.</param>
	/// <param name="errorOutput">The error output.</param>
	public NetResult(string successOutput, string errorOutput)
	{
		this.SuccessOutput = successOutput;
		this.ErrorOutput = errorOutput;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="NetResult"/> class.
	/// </summary>
	public NetResult()
	{
		this.SuccessOutput = "";
		this.ErrorOutput = "";
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: