Archive for the 'Tips & Solutions' Category
Calculating the size of the File in C#
Introduction
This code snippet explains how to calculate the size of the file and display in a label control. For example I am taking PDF file to find the size. It Displays the size of the file in Bytes, Kilobytes and Megabytes.
3 comments//Calculate size of the PDF file
if (PdfFilePath.EndsWith("pdf"))
{
FileInfo info = new FileInfo(PdfFilePath);
long fileSize = info.Length;
string strFileSize = FormatFileSize(fileSize);
lblFileSize.Text = strFileSize;
}
Working with Configuration files
Introduction
Configuration files are central place to configure your web or windows applications. The web.config file is used for configuring the web applications and app.config for windows applications.
The ASP.NET Application comes with web.config file with default configuration sections and you can easily manage them at the intialstage of the project development. As soon as your project grows mature, web.config file turns out to be a huge file.
The configuration files can be different from development environment to production environment.
Example: Connection strings, Application settings, logging and compilation settings are differed from development to production environment.
Maintaining multiple configuration files is pain to manage, For this .NET configuration system has a feature that each configuration section can define an attribute named “configSource” to define an alternative location from where the configuration file has to be loaded.
Example: In web.config file locate the connectionStrings section and add a new attribute name configSource and give the value as “MyConnectionStrings.config”.
1: <connectionStrings configSource="MyConnectionStrings.config"/>
Add a new file called “MyConnectionStrings.config” to the same directory where web.config lives and define its contents as follows:
1: <?xml version="1.0"?>
2: <connectionstrings>
3: <add name="MyDataBase" connectionstring=” ”/>
4: <connectionstrings>
Key points:
- External configuration file must contain the section name as the root element.
- If you use configSource you must not define any other attribute or child element for the respective section in the web.config file.
- It is useful if you move all your configuration files to subfolder to keep root directory clean.
Configuring .NET Framework 1.1 on windows vista
Configuring .NET FW v1.1 on Windows vista is bit tricky.
Here are the steps you need to follow
- Download .NET Fx from .NET FW v1.1 because by default vista come up with .NET FW v2.0 and v3.0.
If you install FW v1.1 alone at the end of installation you will get the following error
RegSvcs.exe - Common Language Runtime Debugging Services
——————————-
Application has generated an exception that could not be handled.
Process id=0×9a0 (2464), Thread id=0xf70 (3952).
Click OK to terminate the application.
Click CANCEL to debug the application.
—————————
OK Cancel
—————————
The above error is in .NET FW v1.1 which is known to occur on window vista.
This issue is resolved in Microsoft .NET FW 1.1 SP1, however you will need to use the following procedure
- Download .NET FW v1.1 SP1 .
- Save both installations to the same directory.
- Ensure that FW 1.1 Redistributable package name is dotnetfx.exe.If not change its name to dotnetfx.exe
- Ensure that FW 1.1 SP1 is named dotnetfxsp1.exe. If not change its name to dotnetfxsp1.exe
- Run the following 3 commands using the order shown
dotnetfx.exe /c:”msiexec.exe /a netfx.msi TARGETDIR=C:\<DirectoryName>”
dotnetfxsp1.exe /Xp:C:\<DirectoryName>\netfxsp.msp
msiexec.exe /a c:\<DirectoryName>\netfx.msi /p c:\<DirectoryName>\netfxsp.msp
- Install both Microsoft .Net Framework 1.1 and Service Pack 1 by running C:\<DirectoryName>\netfx.msi