顯示具有 磁碟空間 標籤的文章。 顯示所有文章
顯示具有 磁碟空間 標籤的文章。 顯示所有文章

2022年5月3日 星期二

[研究][ASP.NET][T-SQL]顯示 SQL Server 2019主機磁碟剩餘空間

[研究][ASP.NET][T-SQL]顯示 SQL Server 2019主機磁碟剩餘空間

2022-05-03

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

SQL Server Management Studio (SSMS) 中執行

DECLARE  @SPACE   AS TABLE  (drive varchar(3), size decimal)
insert into @SPACE EXEC master..xp_fixeddrives
SELECT serverproperty('ServerName') Server_Name,drive,size FROM @SPACE  



EXEC master..xp_fixeddrives  

(下圖)結果


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = @"EXEC master..xp_fixeddrives";
            using (SqlConnection connection = new SqlConnection(
                WebConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Label1.Text += reader["MB 可用"].ToString() + ",";
                    }
                }
            }
        }
    }
}  

執行結果

為什麼要研究這個?因為敝人管理的某系統 Web Server 和 Database Server 是不同主機,也在不同的網段,DB LAN 絕對禁止連 Internet,所以 SQL Server 本身就算有任何 Email 通知機制也沒用;所以想用 ASP.NET 網頁程式取得 DB 主機剩餘空間,檢測剩下多少 GB 時發 Email 通知。

(完)


2021年8月3日 星期二

[研究]Windows Server 2019占用磁碟空間測試

[研究]Windows Server 2019占用磁碟空間測試

2021-08-02

一般管理建立一堆虛擬機器 VM時,倒底磁碟該多大是需要考慮的 ( RAM 和 CPU Core 事後還算好調整,HDD 較麻煩)。

( 下圖) 新安裝的 Windows Server 2019 約 10.2 GB

( 下圖) 新安裝的 Windows Server 2019 約 13.7 GB ( 和上圖不一致,建議參考較大值)
(此圖是以 C: 所有檔案去計算,上圖是以 C: 磁碟去計算)

(下圖) Windows Update 一下,發現 hotfix 只下載更新2021年7月的,之前的不會下載,因為已經包在裡面)



(下圖) 更新後約用 15.1 GB ( Office 並沒有安裝)

********************************************************************************

(下圖) 安裝 Visual Studio Enterprise 2019 v16.10.4,僅勾選「ASP.NET 與網頁程式開發」和「資料儲存和處理」,約需10.59 GB


(下圖) 安裝後,做完 Windows Update,約占用 33.1 GB
( 但 15.1 + 10.59 = 25.69 GB,比預估多了 33.1-25.69 = 7.41 GB)



********************************************************************************
(下圖) 安裝了快 3 年的  Windows Server 2019 + Visual Studio 2019 (有 IIS、沒 Office、網站 Code 在 D: ),約 51.3 GB,比新安裝的多了51.3 - 33.1 = 18.2 GB,可能是 Windows Update 或 Visual Studio 2019 更新時安裝的。




********************************************************************************
敝人測試時,VM 設定為 4GB,C:\ 會有一個隱藏的 pagefile.sys 檔案,就算檔案總管勾選可以檢視「隱藏的項目」和「副檔名」,依然是看不到,這個檔案會因 RAM 大小而不同,所以考慮 C: 磁碟需要的磁碟大小時,要再加上。

根據本篇
pagefile.sys 最大是 3× RAM 或 4 GB (取較大者),敝人新安裝的 VM 目前使用 1.4GB,敝人 VM 的 RAM 設定為 4GB,也就是 pagefile.sys 最多可能 3*4 = 12 GB。(不過實際上成長到那麼大機率非常低,除非你使用的程式需要極大 RAM,遠超真實 RAM,使得 pagefile.sys 變很大。


********************************************************************************

對於沒有 VS2019, Office 的 Windows Server 2019,新安裝 Windows Update 雖只 15.1 GB,考慮 pagefile.sys 和成長期使用 Windows Update 還要占用空間,會建議個 30 GB、40 GB 或 50 GB。

那台用快3年的 Windows Server 2019 + Visual Studio 2019 + Windows Update + Visual Studio 更新,占用 51.3 GB,但檢查  pagefile.sys  只有 704 MB。

使用快 3 年 Win 2019 + SQL 2019 的 C: 約 31 GB (實際 DB 放 D: )

敝人自己使用 Windows Server 2019 + Office (文書編輯、Outlook收發信)+ IIS + SQL Server +Visual Studio 2019 + Windows Update + Visual Studio 更新 C: 實際用 86GB ( 網站Code和實際DB在 D:),建議 C: 最好有 100GB。


(完)

2017年2月17日 星期五

[研究] [C#] [ASP.NET] 取得硬碟剩餘空間 (Windows 2016)

[研究] [C#] [ASP.NET] 取得硬碟剩餘空間 (Windows 2016)

2017-02-17

Visual Studio 2015 with Update 3
Windows Server 2016

測試發行到 Windows Server 2016 的 IIS 上可以跑

Default.aspx.cs
using System;
using System.IO;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                Label1.Text = Label1.Text + "----------------------------------------" + "<br />";
                Label1.Text = Label1.Text + "Drive : " + d.Name + "<br />";
                Label1.Text = Label1.Text + "Drive Type : " + d.DriveType + "<br />";
                if (d.IsReady == true)
                {
                    Label1.Text = Label1.Text + "Volume label : " + d.VolumeLabel + "<br />";
                    Label1.Text = Label1.Text + "File system : " + d.DriveFormat + "<br />";

                    Label1.Text = Label1.Text + "Available space : " + d.AvailableFreeSpace.ToString("###,###,###,###,###.##") + " Bytes <br />";
                    Label1.Text = Label1.Text + "Available space : " + (Decimal.Divide(d.AvailableFreeSpace, 1024) ).ToString("###,###,###,###,###.##") + " KB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + (Decimal.Divide(d.AvailableFreeSpace, 1024) / 1024).ToString("###,###,###,###,###.##") + " MB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + ((double)d.AvailableFreeSpace / 1024 / 1024 / 1024).ToString("###,###,###,###,###.##") + " GB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + (d.AvailableFreeSpace / 1024.0 / 1024 / 1024 / 1024).ToString("###,###,###,###,###.##") + " TB <br /> ";

                    Label1.Text = Label1.Text + "Total available space : " + d.TotalFreeSpace.ToString("###,###,###,###,###") + " Bytes<br />";
                    Label1.Text = Label1.Text + "Total size of drive : " + d.TotalSize.ToString("###,###,###,###,###") + " Bytes<br />";
                }
            }
            Label1.Text = Label1.Text + "----------------------------------------" + "<br />";
        }
    }
}


結果

----------------------------------------
Drive : C:\
Drive Type : Fixed
Volume label :
File system : NTFS
Available space : 47,580,495,872 Bytes
Available space : 46,465,328 KB
Available space : 45,376.3 MB
Available space : 44.31 GB
Available space : .04 TB
Total available space : 47,580,495,872 Bytes
Total size of drive : 125,304,827,904 Bytes
----------------------------------------
Drive : D:\
Drive Type : Fixed
Volume label :
File system : NTFS
Available space : 19,944,185,856 Bytes
Available space : 19,476,744 KB
Available space : 19,020.26 MB
Available space : 18.57 GB
Available space : .02 TB
Total available space : 19,944,185,856 Bytes
Total size of drive : 374,275,567,616 Bytes
----------------------------------------
Drive : E:\
Drive Type : Removable
----------------------------------------
Drive : F:\
Drive Type : Removable
----------------------------------------
Drive : G:\
Drive Type : Removable
----------------------------------------
Drive : H:\
Drive Type : Removable
----------------------------------------
Drive : I:\
Drive Type : CDRom
----------------------------------------


判斷 C: 和 D: 剩餘空間是否小於  10 GB (否則連其他磁碟都測試下去,會判斷錯誤)
bool enough = true;

DriveInfo[] allDrives = DriveInfo.GetDrives();
double space = 0.0;
double driveCSpace = 0.0;
double driveDSpace = 0.0;
foreach (DriveInfo d in allDrives)
{
    if (d.IsReady == true)
    {
        space = d.AvailableFreeSpace / 1024 / 1024 / 1024;   // 換成 GB
        if (d.ToString() == @"C:\")
        {
            driveCSpace = space;
            if (space < 10.0)
                enough = false;
        }
        if (d.ToString() == "D:\\")
        {
            driveDSpace = space;
            if (space < 10.0)
                enough = false;
        }
    }
}

(完)

2016年4月12日 星期二

[研究] [C#][WinForm]顯示所有磁碟的空間

[研究] [C#][WinForm]顯示所有磁碟的空間

2016-04-12

Visual Studio 2015


using System;
using System.Windows.Forms;
using System.IO;

namespace AllDiskSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            GetDeveiceSize();
        }
        private void GetDeveiceSize()
        {
            //取得所有磁碟機的DriveInfo類別
            DriveInfo[] ListDrivesInfo = DriveInfo.GetDrives();
            try
            {
                foreach (DriveInfo vListDrivesInfo in ListDrivesInfo)
                {
                    //使用IsReady屬性判斷裝置是否就緒
                    if (vListDrivesInfo.IsReady)
                    {
                        richTextBox1.AppendText("磁碟代號:" + vListDrivesInfo.Name + "\n");
                        richTextBox1.AppendText("磁碟標籤: " + vListDrivesInfo.VolumeLabel + "\n");
                        richTextBox1.AppendText("磁碟類型:" + vListDrivesInfo.DriveType.ToString() + "\n");
                        richTextBox1.AppendText("磁碟格式:" + vListDrivesInfo.DriveFormat + "\n");
                        richTextBox1.AppendText("磁碟大小:" + vListDrivesInfo.TotalSize.ToString("#,#") + "\n");    // 千分位顯示
                        richTextBox1.AppendText("剩餘空間:" + vListDrivesInfo.AvailableFreeSpace.ToString("#,#") + "\n");
                        richTextBox1.AppendText("總剩餘空間(含磁碟配碟):" + vListDrivesInfo.TotalFreeSpace.ToString("#,#") + "\n");
                        richTextBox1.AppendText("=================================\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ex:" + ex.Message);
            }
        }
    }
}



執行結果


磁碟代號:C:\
磁碟標籤:
磁碟類型:Fixed
磁碟格式:NTFS
磁碟大小:125,304,827,904
剩餘空間:39,481,905,152
總剩餘空間(含磁碟配碟):39,481,905,152
=================================
磁碟代號:D:\
磁碟標籤:
磁碟類型:Fixed
磁碟格式:NTFS
磁碟大小:374,275,567,616
剩餘空間:9,067,077,632
總剩餘空間(含磁碟配碟):9,067,077,632
=================================


(完)