Wednesday, December 24, 2014

Get file information of all files in a folder

Description:
Following code can be used to get file information in a folder:

static void getfileinfo()
{

    string path = "D:\\test\\";
    String[] filenames = Directory.GetFiles(path, "*.txt");
    string file = string.Empty;
    string name = string.Empty;
    DateTime creationTime;
    DateTime lastWriteTime;
    DateTime lastAccessTime;
    foreach (string str in filenames)
    {

        creationTime = File.GetCreationTime(str);
        lastWriteTime = File.GetLastWriteTime(str);
        lastAccessTime = File.GetLastAccessTime(str);

        file = str.Substring(str.LastIndexOf("\\") + 1);
        name = file.Substring(0, file.IndexOf('.')).Replace("-", " ").Trim();
    }

}

Get System Information using C# AvailablePhysicalMemory, AvailableVirtualMemory, InstalledUICulture, OSFullName, OSPlatform, OSVersion PART - 2

Description:
Following code can be used to get system information like AvailablePhysicalMemory, AvailableVirtualMemory, InstalledUICulture, OSFullName, OSPlatform, OSVersion etc..

        static void Main(string[] args)
        {
            Console.WriteLine("AvailablePhysicalMemory = {0}",
            new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory);
            Console.WriteLine("AvailableVirtualMemory = {0}",
            new Microsoft.VisualBasic.Devices.ComputerInfo().AvailableVirtualMemory);
            Console.WriteLine("InstalledUICulture = {0}",
            new Microsoft.VisualBasic.Devices.ComputerInfo().InstalledUICulture);
            Console.WriteLine("OSFullName = {0}",
            new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName);
            Console.WriteLine("OSPlatform = {0}",
            new Microsoft.VisualBasic.Devices.ComputerInfo().OSPlatform);
            Console.WriteLine("OSVersion = {0}",
                        new Microsoft.VisualBasic.Devices.ComputerInfo().OSVersion);
            Console.WriteLine("TotalVirtualMemory = {0}",
                        new Microsoft.VisualBasic.Devices.ComputerInfo().TotalVirtualMemory / (1024 * 1024));
            Console.ReadLine();

        }

Get System Information using C# Operating System, OS Version, 32/64 bit, System Directory, Processor Count, User Name, Domain, Drive information (drive label, drive type, Format, Size, Free space) PART - 1

Description:
Following code can be used to get system information like Operation System, OS Version, 32/64 bit, System Directory, Processor Count, User Name, Domain, Drive information (drive label, drive type, Format, Size, Free space) etc..


private string SystemInformation()
{
    StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
    try
    {
        StringBuilder1.AppendFormat("Operation System:  {0}\n", Environment.OSVersion);
        if (Environment.Is64BitOperatingSystem)
            StringBuilder1.AppendFormat("\t\t  64 Bit Operating System\n");
        else
            StringBuilder1.AppendFormat("\t\t  32 Bit Operating System\n");
        StringBuilder1.AppendFormat("SystemDirectory:  {0}\n", Environment.SystemDirectory);
        StringBuilder1.AppendFormat("ProcessorCount:  {0}\n", Environment.ProcessorCount);
        StringBuilder1.AppendFormat("UserDomainName:  {0}\n", Environment.UserDomainName);
        StringBuilder1.AppendFormat("UserName: {0}\n", Environment.UserName);
        //Drives
        StringBuilder1.AppendFormat("LogicalDrives:\n");
        foreach (System.IO.DriveInfo DriveInfo1 in System.IO.DriveInfo.GetDrives())
        {
            try
            {
                StringBuilder1.AppendFormat("\t Drive: {0}\n\t\t VolumeLabel: " +
                    "{1}\n\t\t DriveType: {2}\n\t\t DriveFormat: {3}\n\t\t " +
                    "TotalSize: {4}\n\t\t AvailableFreeSpace: {5}\n",
                    DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType,
                    DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace);
            }
            catch
            {
            }
        }
        StringBuilder1.AppendFormat("SystemPageSize:  {0}\n", Environment.SystemPageSize);
        StringBuilder1.AppendFormat("Version:  {0}", Environment.Version);
    }
    catch
    {
    }
    return StringBuilder1.ToString();
}
Following function can be used to get free space in a drive:

static void GetTotalFreeSpace()
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady)
            strOutput += drive.Name + " : " + drive.TotalFreeSpace + ",";
    }
}


Check if port is open or not using C#

Description:
System.Net.Sockets.Socket class can be used for Socket Programming. Same class can be used to check if a particular port is open on a particular machine or not. Following code can check this:

static bool TestForOpenPort(string host, int port)
{
    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    try
    {
        s.Connect(host, port);
        return true;
    }
    catch (SocketException)
    {
        return false;
    }
    finally
    {
        ((IDisposable)s).Dispose();
    }
}


Here host is Machine Name or IP Address.

SQL Server 2008 R2 can't connect to local database in Management Studio (Cannot connect to local instance)


Make sure that you are connecting via correct instance name. If your instance is SQLEXPRESS, then you need to use .\SQLEXPRESS or machie-host-name\SQLEXPRESS  as your server name.
And if you have a named instance then put that name as server name.

If this does not solve your problem then you need to check in services if your service is running or not.
Go to Run
 Type services.msc

Go to SQL Server (MSSQLSERVER) service.
Right click service and START

This should resolve your issue.

Write me if you are still facing same issue.