在我的代码中,我有 3 个部分,但是在 3 个部分中的 2 个部分中,代码几乎相同,我想知道是否有人可以帮助我正确地构造它们,这样我只需要在一个范围内编写一次,以便它可以两个部分都可以看到,所以我不需要那么多代码。(框架 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();
}
我想要做的是将代码底部的 SQL to Linq 部分移到它自己的部分中。但要做到这一点,我也必须拥有整个RealDriveInfo
部分..
我想做的另一部分是制作它,List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x=>x.IsReady).ToList<DriveInfo>();
以便 FakeDriveInfo 和 RealDriveInfo 可以看到它。
任何反馈将不胜感激,谢谢。
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 :)