關於網路那些事...

Marketing, SEO, Web trends, Programming tutorial, Web design, and Life event...

C# 用 system.IO 操作檔案 - 教學筆記 (使用visual studio)

使用C# System.IO namespace,可以用在檔案建立、寫入、讀取…

在這裡,主要介紹 System.IO 的 File Class

WriteAllText 寫入/建立檔案

透過WriteAllText可以將文字寫入檔案(如果檔案不存在,會自動建立),格式為:

File.WriteAllText(檔案位置及名稱, 字串);

範例:

string x = "Hello text";
File.WriteAllText("myfilename.txt", x);

ReadAllText 讀取文件

使用ReadAllText可以直接讀取文件中的內容,格式為:

File.ReadAllText(檔案位置及名稱);

建立檔案 & 讀取檔案 範例:

//建立檔案
string x = "Hello text";
File.WriteAllText("myfilename.txt", x);
//讀取檔案
string y = File.ReadAllText("myfilename.txt");
Console.WriteLine(y); 

AppendAllText 插入文字

C# 將字串插入文件內容尾端 範例:

File.AppendAllText("myfilename.txt", " append text to the end.");
string y = File.ReadAllText("myfilename.txt");
Console.WriteLine(y);

Exists 判斷檔案是否存在

透過 Exists 可以判斷檔案是否已經存在 返回布林值

File.Exists(檔案位置及名稱);

範例

if (File.Exists("myfilename.txt")) {
    Console.WriteLine("Yes");
}else{
    Console.WriteLine("No");
}

Delete 刪除檔案

刪除指定的檔案

Continue Reading

C# try-catch 例外處理(Execption Handling) - 教學筆記 (使用visual studio)

例外處理,可以處理一些未知且發生錯誤的情況,例如: 輸入不合法(型別不符)的值、找不到要開啟的資料、處理資料時突然失去連線…。

在C# 例外處理使用 try-catch 陳述式

只要將一般陳述式放在try區塊,

當try區塊發生發生時,就會移向catch區塊,用Exception擷取錯誤

Continue Reading

C# 結構(Structs) - 教學筆記 (使用visual studio)

C# 的 Struct 是一個value type的小群組,

在管理程式變數時,非常好用

例如,對於註冊用戶資料,會包含姓名、性別、住址…. 透過struct,就可以將用戶資料統整成一個群組結構

Continue Reading

C# 運算子實現類別多載 - 教學筆記 (使用visual studio)

Operator Overloading on Class

在C#設計Class,可以搭配運算子(operator)來做到多載(overloading) 並且,要Overloading的運算子,必須是 static 例如:

        class Box
        {
            public int Height { get; set; }
            public int Width { get; set; }

            public Box(int h, int w)
            {
                Height = h;
                Width = w;
            }
            public static Box operator +(Box a, Box b)
            {
                int h = a.Height + b.Height;
                int w = a.Width + b.Width;
                Box res = new Box(h, w);
                return res;
            }
        }

        static void Main(string[] args)
        {
            Box b1 = new Box(14, 3);
            Box b2 = new Box(5, 7);
            Box b3 = b1 + b2;
            Console.WriteLine(b3.Height);
            Console.WriteLine(b3.Width);
        }

底下範例中,實現多載 operator +, operator -, operator *, operator /

Continue Reading

C# Readonly - 教學筆記 (使用visual studio)

readonly與常數用法很像, 相較於常數,readonly通常會用在class流程設計 readonly 在建構子指派值之後,就不能更改

readonly可以在建構子裡面修改

Continue Reading

C# Math, String, Array, Datetime 類別 - 教學筆記 (使用visual studio)

在這裡記錄一些常用的 C# 內置的靜態類別

Continue Reading

C# 解構子 Destructors - 教學筆記 (使用visual studio)

前面我們有提到,只要class被實例化(instantiated),當下就會立即執行建構子 而這裡要說明的解構子(destructor),也是在class被實例化時,會被執行

解構子,具有下列特性: - 一個class只能有一個解構子 - 會在class執行完成,準備結束時自動被調用 - 無法被呼叫 - 不接受修飾符及參數

Continue Reading

C# get & set 存取子(accessors) - 教學筆記 (使用visual studio)

C# get & set 存取子 class的屬性(Property)可以讓讀、寫、運算機制變得更有彈性, 在這裡要介紹屬性的一個特殊的methods,稱為: 存取子(accessors),也可稱為訪問器 存取子包含 get存取子、set存取子

Continue Reading

C# 建構子 (Constructors) - 教學筆記 (使用visual studio)

C# 建構子 Constructors 說明 建構子是一個特殊的Method成員, 只要class被實例化,當下就會立即執行建構子(自動被調用)

在C#中,建構子的名稱必須和class一致,並且不需要定義類型 並且,可以傳入參數

Continue Reading

C# 繼承 (Inheritance) - 教學筆記 (使用visual studio)

C# 繼承(Inheritance) Class 說明

物件導向的方便之處在於,可以被拿來重複使用及擴充 例如,今天新增了使用者檢視列表Class 如果在無法更動情況下,想要擴充新增使用者功能 就可以用繼承(Inheritance)的方式 而被繼承的Class,稱之為「基底類別」(Base Class)

但也要特別留意! C#不支援多重繼承 所以,只允許繼承一個「基底類別」(Base Class)

Continue Reading

C# Class, Object (static, public, protected, private)介紹 - 教學筆記 (使用visual studio)

在這裡,會介紹C#的Class 及 Object基本用法

Class基本架構

物件導向開發時,會運用class來作為資料型別

這時,class就好像一個大類別,在這裡面,開發者通常會將具有關聯性的成員(Object、Methods、variable..)統合起來

Continue Reading

C# Method - Main、void、ref、out、overloading - 教學筆記 (使用visual studio)

在 C# 中,除了可以原本的Method,你也可以定義自己的Method

Method具有以下優點: - 可重複使用。 - 易於測試。 - 對方法的修改不影響調用程序。 - 一種方法可以接受許多不同的輸入。

Continue Reading

C# break and continue - 教學筆記 (使用visual studio)

在設計程式過程中,透過continue及break,可以讓程式更有彈性

break

break主要的任務就是用來跳出迴圈或者陳述式 在流程控制中的 switch ,就包含了break 也就是,當switch滿足條件時,就能透過break來跳脫迴圈 如果是用在邏輯演算,同樣也可以搭配break來跳脫 例如,取出五筆資料

Continue Reading

C# while、for、do-while 迴圈 - 教學筆記 (使用visual studio)

迴圈,一般可以形容在特定條件中,持續重複同一件事情 在程式設計過程,時常會運用迴圈來進行計算或取資料

在這裡,會介紹幾種迴圈 while、for、do-while 及實作

Continue Reading

C# 字串 String - 教學筆記 (使用visual studio)

在 C# ,strings 是以物件(Objects)形式存在。 因此,當我們宣告 string 時,事實上是實例化(instantiate)一個字串物件。

另外,C# 提供了許多方法給string使用, 讓我們可以很方便的分析及處理字串相關的計算

Continue Reading

C# Array 陣列 - 教學筆記 (使用visual studio)

C#提供了許多預設class可以用來儲存資料,其中一個就是array陣列 陣列可以用來儲存多組資料。 在應用於文章、使用者資料、權限管理等介面時,可以先將值儲存成陣列,再透過迴圈的方式一個一個取出來。(迴圈的用法將會在後續提到)

在這裡,會用比較簡潔易懂的方式來解說, 建議可以先從本篇理解陣列用法,再前往官網參考手冊參考更仔細的寫法

Continue Reading

C# 流程控制 - 教學筆記 (使用visual studio)

在前面,主要介紹數學運算,在這裡會先介紹常見的邏輯運算 然後才會帶入流程控制相關介紹

邏輯運算

邏輯運算主要型別為布林(bool),會返回True、false或null 在流程控制過程中,相當好用!

Continue Reading

C# 運算式(operators) - 教學筆記 (使用visual studio)

運算式(operators) 可以用來進行數學演算或邏輯演算

在C#,各種運算式都能用符號來表示

數學運算式

這裡列出C#支援的數學演算運算符

Continue Reading

C# 變數、常數- 教學筆記 (使用visual studio)

C#是一個強型別語言,在建立C#變數或常數時,要同時宣告它的類別型別(type) 也就是說,你必需告知建立的變數是要用來儲存數字、文字還是其他…等

Continue Reading

C# 輸入及讀取文字 - 教學筆記 (使用visual studio)

學習C#程式過程中,visual studio提供了Console類別 (無法被繼承)讓我們可以輸入、輸出及讀取訊息

這裡主要說明關於輸入及讀取文字的方式

Continue Reading