C Sharp - Sizuha/devdog GitHub Wiki
๊ธฐ๋ณธ์ ์ผ๋ก C++๊ณผ ๋น์ทํ๋ฏ๋ก, ์ฌ๊ธฐ์๋ C#์์์ ์ฐจ์ด์ ๋ง ์ค๋ช ํจ.
C# ๋ฌธ๋ฒ ์ฐธ์กฐ.
public class DirFlags {
public bool up;
public bool down;
public bool left;
public bool right;
public DirFlags(bool u=false, bool d=false, bool l=false, bool r=false) {
up = u;
down = d;
left = l;
right = r;
}
}
// p2 -> p1
public DirFlags GetMoveDir() {
Vector3 p1;
Vector3 p2;
GetLastVectors(out p1, out p2);
return new DirFlags(p1.y > p2.y, p1.y < p2.y, p1.x < p2.x, p1.x > p2.x );
}
์ด ์ฝ๋๋ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋๋ค.
๋ค์ ๋ถ๋ถ์ ์ ๋๋ฆญ ๋ฌธ๋ฒ์ผ๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
p1.x < p2.x, p1.x > p2.x
์ด ์ค๋ฅ๋ฅผ ํผํ๋ ค๋ฉด, ํ๋ผ๋งคํฐ๋ฅผ ๊ฐ๊ฐ ๋ฐ๋ก๋ฐ๋ก ๋ณ์์ ๋ด๊ฑฐ๋, ๊ดํธ๋ฅผ ๋ฌถ์ด์ ์ปดํ์ผ๋ฌ์๊ฒ ๋ฌธ๋ฒ์ ์ฐ์ ์์๋ฅผ ์ง์ ํด ์ค ์ ๋ฐ์ ์๋ค.
IEnumerator๊ฐ์ iterator ์์ฑ์ ๊ฐ์ง ๋งค์๋์์๋ out/ref ํ๋ผ๋งคํฐ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
System.Enum ํด๋์ค์ static ๋งค์๋๋ฅผ ํ์ฉํ๋ฉด, enum์ผ๋ก ์ ์๋ ํ์ ์ด ๊ฐ์ง๊ณ ์๋ ๊ฐ ํน์ ์ด๋ฆ๋ค์ ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค. ๋ฌผ๋ก ๊ทธ ๋ฆฌ์คํธ๋ฅผ ํตํด์ foreach๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ฐ๋ฅ.
public enum Suits
{
Spades,
Hearts,
Clubs,
Diamonds,
NumSuits
}
public void PrintAllSuits()
{
foreach (var suit in System.Enum.GetValues(typeof(Suits)))
{
System.Console.WriteLine(suit.ToString());
}
}
private static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long GetCurrentUnixTimestampMillis()
{
return (long) (DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
}
public static DateTime DateTimeFromUnixTimestampMillis(long millis)
{
return UnixEpoch.AddMilliseconds(millis);
}
public static long GetCurrentUnixTimestampSeconds()
{
return (long) (DateTime.UtcNow - UnixEpoch).TotalSeconds;
}
public static DateTime DateTimeFromUnixTimestampSeconds(long seconds)
{
return UnixEpoch.AddSeconds(seconds);
}
DateTime local = (DateTime_Object).ToLocalTime();
DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] files = root.GetFiles("*.xxx");
foreach (FileInfo f in files) {
StreamReader input = f.OpenText();
// . . .
}
bool is_file_exist = System.IO.File.Exists(file_path);
bool is_dir_exist = System.IO.Directory.Exists(path);
Directory.CreateDirectory("path");
Path์์ ๋ชจ๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ๋ค.
System.IO.Directory.Delete(path);
System.IO.Directory.Delete(path, true); // ์ด ๊ฒฝ์ฐ, ์๋ธ ๋๋ ํ ๋ฆฌ๊น์ง ์ง์.
ํ ์คํธ ํ์ผ ์ ์ฒด๋ฅผ ์ฝ์ด์ค๊ฑฐ๋, ๋ฌธ์์ด ์ ์ฒด๋ฅผ ํ ์คํธ ํ์ผ๋ก ์ ์ฅํ๊ณ ์ ํ ๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ๋จํ ์ฒ๋ฆฌ ๊ฐ๋ฅ.
using System.IO;
// ์ด๋ฏธ ํ์ผ์ด ์กด์ฌํ ๊ฒฝ์ฐ์๋ ๊ธฐ์กด ํ์ผ์ ๋ด์ฉ์ ์ง์์ง๊ณ ์๋ก์ด ๋ด์ฉ์ผ๋ก ๋ฎ์ด์ฐ๊ฒ ๋๋ค.
File.WriteAllText(path, createText);
File.ReadAllText(path);
using System.IO;
StreamWriter sw = File.CreateText(path);
sw.WriteLine("test");
sw.Close();
File.CreateText()๋ append ๋ชจ๋๊ฐ false๋ก ์ค์ ๋ StreamWrite๋ฅผ ์์ฑํ๋ค. ๋ฐ๋ผ์, ์ด๋ฏธ ํ์ผ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ ๋ฎ์ด ์ฐ๊ธฐ๋ฅผ ํ๊ฒ ๋๋ค. Encoding์ UTF-8๋ก ์ค์ ๋๋ค.
using System.IO;
StreamReader sr = File.OpenText(path);
string s = sr.ReadLine();
using System.IO;
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result);
// This code produces output similar to the following:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''
using System.Xml;
XmlTextReader reader = new XmlTextReader("books.xml");
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
// Read attributes.
if (reader.HasAttributes) {
Console.WriteLine("Attributes of <" + reader.Name + ">");
while (reader.MoveToNextAttribute()) {
Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
}
// Move the reader back to the element node.
reader.MoveToElement();
}
}
reader.Close();
using System.Xml;
XmlTextReader reader = new XmlTextReader("patch_list.xml");
XmlDocument xml = new XmlDocument ();
xml.Load(reader);
XmlElement xmlRoot = xml.DocumentElement;
// Root
foreach (XmlNode node in xmlRoot.ChildNodes) {
string nodeName = node.Name;
XmlAttribute attr = node.Attributes["Attribute Name"].Value; // ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ.
string value = node.value; // <node>value</node> ํํ์ธ ๊ฒฝ์ฐ.
string innerText = node.InnerText; // value ๋ถ๋ถ์ด ์ฌ๋ฌํ์ผ๋ก ๋์ด ์๋ ๊ฒฝ์ฐ.
}
reader.Close();
Using LINQ-to-XML, you can do var doc = XDocument.Load("yourfilepath"). From there its just a matter of querying the data you want, say like this:
var authors = doc.Root.Elements().Select( x => x.Element("Author") );
using System;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main( string[] args )
{
XDocument doc = XDocument.Load( "XMLFile1.xml" );
var authors = doc.Descendants( "Author" );
foreach ( var author in authors )
{
Console.WriteLine( author.Value );
}
Console.ReadLine();
}
}
}
http://stackoverflow.com/questions/4015324/http-request-with-post
using System.Net;
using System.Collections.Specialized;
// POST
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["thing1"] = "hello";
values["thing2"] = "world";
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);
}
// GET
using (var client = new WebClient())
{
var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}
using System.Security.Cryptography;
using System.Text;
using System;
public class CryptoLib {
// 128bit(16byte)ใฎIV๏ผๅๆใใฏใฟ๏ผใจKey๏ผๆๅทใญใผ๏ผ...
private const string iv = @"1234567890ABCDEF";
private const string key = @"1234567890ABCDEF";
/// <summary>
/// ๆๅญๅใAESใงๆๅทๅ.
/// </summary>
public static string Encrypt(string text)
{
// AESๆๅทๅใตใผใในใใญใใคใ.
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// ๆๅญๅใใใคใๅ้
ๅใซๅคๆ.
byte[] src = Encoding.Unicode.GetBytes(text);
// ๆๅทๅใใ.
using (ICryptoTransform encrypt = aes.CreateEncryptor())
{
byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
// ใใคใๅ้
ๅใใBase64ๅฝขๅผใฎๆๅญๅใซๅคๆ.
return Convert.ToBase64String(dest);
}
}
/// <summary>
/// ๆๅญๅใAESใงๅพฉๅทๅ.
/// </summary>
public static string Decrypt(string text)
{
// AESๆๅทๅใตใผใในใใญใใคใ.
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Base64ๅฝขๅผใฎๆๅญๅใใใใคใๅ้
ๅใซๅคๆ.
byte[] src = System.Convert.FromBase64String(text);
// ่คๅทๅใใ.
using (ICryptoTransform decrypt = aes.CreateDecryptor())
{
byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
return Encoding.Unicode.GetString(dest);
}
}
}
System.Diagnostics.Process.Start("process-name", "parameters");
// ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ๋๊ธฐ
System.Diagnostics.Process.Start("process-name", "parameters").WaitForExit();
์ฐธ๊ณ
IEnumerable<T>
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋์์ ๋ํด ์ ์ฉ ๊ฐ๋ฅ.
using System.Linq;
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery = from num in numbers where (num % 2) == 0 select num;
// 3. Query execution.
foreach (int num in numQuery) Console.Write("{0,1} ", num);
using System.Linq;
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// numQuery is an IEnumerable<int>
var numQuery = numbers.Where(num => num % 2 == 0);