Get assembly version & other attributes

To get assembly version:

var systemVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

To get assembly file attribute:

string company = "Default";
var ass = Assembly.GetExecutingAssembly();
var att = ass.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (att.Length > 0)
{
var companyAtt = att[0] as AssemblyCompanyAttribute;
company = companyAtt.Company;
}

Advertisements
Posted in C#. Tags: . Leave a Comment »

Workflow Tracking DB Monitor

A sample console application that lists workflows information from the WorkflowTracking database:

Workflow Tracking DB Monitor Project

Using SharpBITS to transfer (download) file

1. Add a reference to the SharpBITS.dll on your project (download from SharpBITS.NET).
2. Add multi-line TextBox named “output” to your form.
3. Add following piece of code to your form code:

public partial class BITStester : Form
{

BitsManager manager = null;
object jobTransferredSync = new object();
object jobFailedSync = new object();
const string fromURL = @"http://localhost/download/test.txt";
const string fileTo = @"c:/temp/test.txt";

private void BITStester_Load(object sender, EventArgs e)
{
	manager = new BitsManager();
	manager.OnJobTransferred += new EventHandler(OnJobTransferred);
	manager.OnJobError += new EventHandler(OnJobError);
	manager.EnumJobs(JobOwner.AllUsers);	

	FileInfo file = new FileInfo(fileTo);

	BitsJob job = manager.CreateJob(file.Name, JobType.Download);

	job.NotificationFlags = NotificationFlags.JobTransferred;

	// The default retry delay was 600 seconds (10 minutes).
	job.MinimumRetryDelay = 60; // 1 min
	job.Priority = JobPriority.High;
	job.Description = "Created by BITStester";
	job.AddFile(fromURL, fileTo);
	job.Resume();

	AddToOutput("File {0} added to BITS pool", file.Name);
}

private void OnJobTransferred(object sender, NotificationEventArgs e)
{
	lock (jobTransferredSync)
	{
		BitsJob job = e.Job;
		job.Complete();
		AddToOutputHelper(string.Format("File {0} downloaded", job.DisplayName));
	}
}

private void OnJobError(object sender, NotificationEventArgs e)
{
	lock (jobFailedSync)
	{
		BitsJob job = e.Job;
		string errorMessage = string.IsNullOrEmpty(job.Description) ? 
				"Error description not provided" : job.Error.Description;

		AddToOutputHelper(string.Format("File download {0} failed: {1}", 
				job.DisplayName, errorMessage));
		job.Cancel();
	}
}

#region Output

private delegate void UpdateOutputHandler(string line);

// To call also from another thread
private void AddToOutputHelper(string line)
{
	if (output.InvokeRequired)
	{
		output.Invoke(new UpdateOutputHandler(AddToOutput), line);
	}
	else
	{
		AddToOutput(line);
	}
}

private void AddToOutput(string line)
{
	output.AppendText(line + Environment.NewLine);
}

private void AddToOutput(string line, params object[] param)
{
	output.AppendText(string.Format(line, param) + Environment.NewLine);
}

#endregion Output
} // class

 

Useful links when using BITS technology:

Posted in BITS, C#. Tags: , , , , . Leave a Comment »

Encrupt/Protect Custom Configuration Section

Illustrates how to encrypt custom configuration section in the service OnStart event:

protected override void OnStart(string[] args)
{
	try
	{			    
		string sectionName = "SectionNameToSecure";
		
		Configuration config = 
				ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
		ConfigurationSection section = config.GetSection(sectionName);
		if (!section.SectionInformation.IsProtected)
		{
			section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
			section.SectionInformation.ForceSave = true;
			config.Save(ConfigurationSaveMode.Full);

			// Reset configuration object!
			ConfigurationManager.RefreshSection(sectionName);
		}                
	}
	catch (Exception ex)
	{
		// Do exception tracing here
		this.Stop();
	}
}

C# – Convert Bytes to MB

The one-line code that converts bytes to megabytes as formatted string:

long fileSizeInBytes = new Random.Next(1, 1024 + 1);
string fileSize = string.Format("{0:0.00} MB", (fileSizeInBytes / 1024f) / 1024f);

Posted in C#. Tags: , . Leave a Comment »