程序員的1111


又是一年一度的雙11,購物的購物,約會的約會,秀恩愛的秀恩愛,單身汪自然不屬於這三者。想想,沒什么好買的。約會,秀恩愛那種更沒了。前幾年說,希望明年光棍節前可以脫單,結果到現在還是一個人過。誒,說多了都是淚。但是,人總是要有夢想的,希望明年光棍節前可以擺脫單身的狀況。

 

”未將對象引用設置到對象的實例“

image

這種情況,大家都遇到過,通俗一點就是”找不到對象“,程序員經常跟”對象“打交道,卻總是找不到對象,只能用new一個”對象“來自嘲了。

image

像是這種情況,更是給人10000點傷害,連Excel都嘲笑你找不到對象,還能不能讓人愉快的工作了。

既然這樣,在這個日子,自娛自樂自嘲一番,想必是極好的,於是就有了下面這個

image

雖然不知道該叫什么,應下景的話,那暫且叫它”my1111“

 

如何實現的呢,原理就是往磁盤塞一定量數據。一定量數據=目前磁盤可用空間-最后剩余可用空間

代碼比較簡單,直接貼出來

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DiskFiller
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowDrives();//顯示每個盤的信息
            List<FillingDrive> userInputFillingDrive = CollectUserInput();//獲取輸入的每個盤和要設置的可用空間
            Console.WriteLine("填充中");
            List<string> filledFilePathList = FillDataToDrive(userInputFillingDrive);//往磁盤填充數據
            Console.WriteLine("填充完成");
            Console.WriteLine("是否刪除填充的數據(輸入y表示刪除):");
            if (Console.ReadLine().ToLower() == "y")
            {
                DeleteUselessData(filledFilePathList); //刪除填充的數據
            }
        }

        /// <summary>
        /// 顯示每個盤的信息
        /// </summary>
        public static void ShowDrives()
        {
            IEnumerable<DriveInfo> fixedDiskDrives = GetFixedDiskDrives();
            foreach (var drive in fixedDiskDrives)
            {
                Console.WriteLine("盤符:" + drive.Name + "  可用空間:" + (drive.TotalFreeSpace >> 20).ToString() + "MB");
            }
        }

        /// <summary>
        /// 獲取輸入的每個盤和要設置的可用空間
        /// </summary>
        /// <returns></returns>
        public static List<FillingDrive> CollectUserInput()
        {
            List<FillingDrive> fillingDriveList = new List<FillingDrive>();
            Console.WriteLine("輸入的格式為  盤符,設置的可用空間  \n例如: c,15    表示要設置C盤,並且設置C盤的最后可用空間是15MB ");
            while (true)
            {
                Console.WriteLine("請輸入,輸入quit結束輸入:");
                string input = Console.ReadLine();
                if (input.ToLower() == "quit")
                {
                    break;
                }
                string[] inputArray = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                fillingDriveList.Add(new FillingDrive() //這里沒判斷輸入的格式是否正確
                    {
                        DriveName = inputArray[0],
                        NewFreeSpace = Convert.ToInt64(inputArray[1]) << 20
                     }
                    );
            }
            return fillingDriveList;
        }

        /// <summary>
        /// 往磁盤填充數據
        /// </summary>
        /// <param name="fillingDrives"></param>
        /// <returns></returns>
        public static List<string> FillDataToDrive(List<FillingDrive> fillingDrives)
        {
            List<string> filledFilePathList = new List<string>();
            foreach (var item in fillingDrives)
            {
                DriveInfo drive = new DriveInfo(item.DriveName);
                string filledFileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".my1111";//填充數據的文件
                string filledFilePath = Path.Combine(drive.RootDirectory.FullName, filledFileName);
                using (FileStream fs = new FileStream(filledFilePath, FileMode.OpenOrCreate))
                {
                    //減去4096是因為設置系統盤時,最后的結果總是多填充了4096字節,所以這里少填充4096字節
                    long fillingSpace = drive.TotalFreeSpace - item.NewFreeSpace -4096;
                    fs.SetLength(fillingSpace);

                } 
                filledFilePathList.Add(filledFilePath);
            }
            return filledFilePathList;
        }

        /// <summary>
        /// 刪除填充的數據
        /// </summary>
        /// <param name="filledFilePathList"></param>
        public static void DeleteUselessData(List<string> filledFilePathList)
        {
            foreach (var path in filledFilePathList)
            {
                File.Delete(path);
            }
        }

        /// <summary>
        /// 獲取磁盤
        /// </summary>
        /// <returns></returns>
        internal static IEnumerable<DriveInfo> GetFixedDiskDrives()
        {
            DriveInfo[] totalDrives = DriveInfo.GetDrives();
            IEnumerable<DriveInfo> fixedDiskDrives = totalDrives.Where(p => p.DriveType == DriveType.Fixed);
            return fixedDiskDrives;
        }

        public class FillingDrive
        {
            /// <summary>
            /// 盤符
            /// </summary>
            public string DriveName { get; set; }

            /// <summary>
            /// 填充到指定大小
            /// </summary>
            public long NewFreeSpace { get; set; }
        }
    }
}

運行前,看下各盤的的情況,還需要給一個干凈的運行環境,防止其他軟件在磁盤生成數據文件,造成最后結果不准確。

image

執行

image

輸入各盤最后可用空間,輸入quit結束輸入

image

按回車,看到“填充完成”表示已經填完了

image

接下來,看下各盤的情況

image

可以看到各盤可用空間是輸入的剩余可用空間大小

各盤生成了一個后綴名為“my1111”的文件,比如,這里的E盤

image

看完結果后,輸入”y”刪除填充的文件,可以看到空間又回來了,如果輸入了其它,程序會退出,填充的文件只能手動刪,

刪除后綴是“my1111”的文件就可以了。

image

 

最后,祝各位1111快樂。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM