1. Finding the location of the executing assembly
String exePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
2. Support memory objects > 2 GB
<runtime>
<gcAllowVeryLargeObjects enabled ="true" />
</runtime>
More more, refer – Handling large objects in .net ( C# )
3. Parallel processing and using Task factory
BlockingCollection buffer1 = new BlockingCollection();
PrepareData(ConfiguationValues,buffer1);
BlockingCollection buffer2 = new BlockingCollection();
........................
........................
BlockingCollection buffer6 = new BlockingCollection();
TaskFactory factory = new TaskFactory(TaskCreationOptions.LongRunning,TaskContinuationOptions.None);
Task stage2 = factory.StartNew(() => CreateCoverSheet(buffer1, buffer2, ConfiguationValues));
............................
............................
Task stage6 = factory.StartNew(() => MergePDf(buffer5, buffer6, ConfiguationValues));
4. Create zip file from 2 files
Add reference to the following two dlls
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.dll
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll
using System.IO.Compression;
ZipArchive zip = ZipFile.Open(zip_file_path_and_name, ZipArchiveMode.Create);
zip.CreateEntryFromFile(file1Path, file1Name, CompressionLevel.Optimal);
zip.CreateEntryFromFile(file2Path, file1Name, CompressionLevel.Optimal);
zip.Dispose();
5. Time taken to execute a program
var watch = Stopwatch.StartNew();
//name of the function to time
watch.Stop();
Console.WriteLine("Total time taken " + watch.Elapsed);
6. XML Validation
using (XmlReader xr = XmlReader.Create(xmlFileName))
{
try
{
while (xr.Read()) { }
Console.WriteLine("XML validation PASSED for xml file '" + xmlFileName + "'");
}
catch (Exception ex)
{
Console.WriteLine("XML validation FAILED for xml file'+ xmlFileName + "'. Details: " + ex.Message);
}
}
7. Read from Configuration file
Add the dll ‘System.Configuration.dll‘ as a reference to the project
using System.Configuration;
ConfigurationManager.AppSettings.Get("setting name");
In the configuration file ( app.config), add the following lines,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="LogOutput" value="file" />
</appSettings>
</configuration>
8. Check for path validity
public static string checkForPathValidity(params string[] paths)
{
string missingFolder = "";
foreach (string path in paths)
{
missingFolder += checkDir(path);
}
return missingFolder;
}
9. Calling an external executable
The following code calls an external executable and retrieves the output ( exit 1 or 0 ) which is printed to the console and returns it back to the program
public string callExternalExecutable(string exePath, string arguments)
{
string consoleOutput = "";
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = exePath,
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
process.Start();
using (StreamReader reader = process.StandardOutput)
{
consoleOutput = reader.ReadToEnd();
}
return consoleOutput;
}
catch (Exception ex)
{
throw ex;
}
}