Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8343563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:06:10+00:00 2026-06-09T06:06:10+00:00

In my code I have 3 sections, but in 2 of the 3 sections

  • 0

In my code I have 3 sections, but in 2 of the 3 sections the code is almost identical, I was wondering if anyone could help me structure them properly so that I would only need one line written once, in a scope so that it can be seen by both sections so I don’t need to have so much code. (Framework 3.5)

public static void FakeDriveInfo()
{
    List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x => x.IsReady).ToList<DriveInfo>();

    Server server = new Server();

    Console.WriteLine();
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("Server ID :      {0}", server.ServerID = 0);
    Console.WriteLine("Server Name :    {0}", server.ServerName = string.Concat(System.Environment.MachineName));
    Console.WriteLine();

    for (int i = 0; i < driveList.Count; i++)
    {
        ServerDrive serverDrives = new ServerDrive();

        Console.WriteLine("Drive Letter:    {0}", driveList[i].Name);
        Console.WriteLine("Total Size:      {0}", FormatBytes(driveList[i].TotalSize));
        Console.WriteLine("Volume Label:    {0}", driveList[i].VolumeLabel);
        Console.WriteLine("Free Space:      {0}", FormatBytes(driveList[i].TotalFreeSpace));
        Console.WriteLine("Drive Format:    {0}", driveList[i].DriveFormat);
        Console.ReadLine();
    }
}

public static void RealDriveInfo()
{
    //Create the server object - You will need create a list of the server objects.
    Server server = new Server();

    //Get all drives information
    List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x => x.IsReady).ToList<DriveInfo>();

    //Insert information of one server - You will need get information of all servers
    server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
    server.ServerName = string.Concat(System.Environment.MachineName);

    //Inserts information in the newServers object
    for (int i = 0; i < driveList.Count; i++)
    {
        ServerDrive serverDrives = new ServerDrive();

        //Put here all the information to obeject Server                
        serverDrives.DriveLetter = driveList[i].Name;
        serverDrives.TotalSpace = driveList[i].TotalSize;
        serverDrives.DriveLabel = driveList[i].VolumeLabel;
        serverDrives.FreeSpace = driveList[i].TotalFreeSpace;
        serverDrives.DriveType = driveList[i].DriveFormat;

        //      server.ListServerDrives.Add(serverDrives);
        server.ServerDrives.Add(serverDrives);
    }

    //Add the information to an SQL Database using Linq.
    DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver");
    //   db.Servers.InsertAllOnSubmit(server);
    db.Servers.InsertOnSubmit(server);
    db.SubmitChanges();
}

What I want to do is move the SQL to Linq part at the bottom of the code into it’s own section. But to do that I have to have the whole RealDriveInfo Section too..

Another part that I want to do is make the List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x=>x.IsReady).ToList<DriveInfo>(); so that it can be seen by FakeDriveInfo and RealDriveInfo..

Any feedback would be greatly appreciated, thanks.

EDIT : At the moment I am calling two methods,
FakeDriveInfo(); = Executing the console app and showing me the info it is going to submit.
Name, Letter, Label, Server Name, ID, etc.
RealDriveInfo(); = Connecting to the SQL Server and inserting the information into the two tables.

I want to have a third method
WriteInToDB(); = The DB Writing code would be taken from the RealDriveInfo Method and moved here instead.

At the moment I have two methods that practically look identical due to the scope.
I want the scope shifted so that I can put the List part of the code into the Main() Method and both FakeDriveInfo and RealDriveInfo can use it from there instead of having the code duplicated.

Hopefully this adds a bit more sense to it all 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:06:13+00:00Added an answer on June 9, 2026 at 6:06 am

    After pain-stakeingly realising, the answer was in method and class scope..

    Just incase anyone would like to see what I did ::

    public class Program
    {
        List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x => x.IsReady).ToList<DriveInfo>(); //Get all the drive info
        Server server = new Server();  //Create  the server object
        ServerDrive serverDrives = new ServerDrive();
    
        public void Main()
        {
            Program c = new Program();
            c.FakeDriveInfo();
            c.RealDriveInfo();
            c.WriteInToDB();
        }
    
        public static string FormatBytes(long bytes)
        {
            const int scale = 1024;
            string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
            long max = (long)Math.Pow(scale, orders.Length - 1);
            foreach (string order in orders)
            {
                if (bytes > max)
                {
                    return string.Format("{0:##.##} {1}", Decimal.Divide(bytes, max), order);
                }
    
                max /= scale;
            }
            return "0 Bytes";
        }
    
        public void FakeDriveInfo()
            {
    
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Server ID :      {0}", server.ServerID = 0);
                Console.WriteLine("Server Name :    {0}", server.ServerName = string.Concat(System.Environment.MachineName));
                Console.WriteLine();
    
    
                for (int i = 0; i < driveList.Count; i++)
                {
    
                    Console.WriteLine("Drive Letter:    {0}", driveList[i].Name);
                    Console.WriteLine("Total Size:      {0}", FormatBytes(driveList[i].TotalSize));
                    Console.WriteLine("Volume Label:    {0}", driveList[i].VolumeLabel);
                    Console.WriteLine("Free Space:      {0}", FormatBytes(driveList[i].TotalFreeSpace));
                    Console.WriteLine("Drive Format:    {0}", driveList[i].DriveFormat);
                    Console.ReadLine();
                }
            }
    
        public void RealDriveInfo()
             {
    
    
                //Insert information of one server - You will need get information of all servers
                server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
                server.ServerName = string.Concat(System.Environment.MachineName);
    
                //Inserts information in the newServers object
                for (int i = 0; i < driveList.Count; i++)
                {
    
                    //Put here all the information to obeject Server                
                    serverDrives.DriveLetter = driveList[i].Name;
                    serverDrives.TotalSpace = driveList[i].TotalSize;
                    serverDrives.DriveLabel = driveList[i].VolumeLabel;
                    serverDrives.FreeSpace = driveList[i].TotalFreeSpace;
                    serverDrives.DriveType = driveList[i].DriveFormat;
                    server.ServerDrives.Add(serverDrives);
    
                }
             }
    
        public void WriteInToDB()
        {
            //Add the information to an SQL Database using Linq.
            DataClasses1DataContext db = new DataClasses1DataContext(@"cspsqldev");
            //   db.Servers.InsertAllOnSubmit(server);
            db.Servers.InsertOnSubmit(server);
            db.SubmitChanges();
        }
    

    Simply by moving the parts I wanted to be seen by all methods, into the Class scope it now works.

    Thanks for your feedback guys 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Javascript I have code that will hide various sections in a MS CRM form
well i mean i have several sections of code that use some variable i
I have several sections of code that I need to protect with a Mutex.
I have the following code (only sections of it) for getting the entries in
I have the following json code file named: sections.json { section1: { priority: 1,
I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
Almost all of the Delphi code I have read has all the class type
hey there guys and girls i have this code that saves json as a
I have various sections within a page of content that dynamically get replaced based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.