File - static Method 만 제공 File.으로 접근 매 호출 시 security 검사
string filePath = @"C:\fourthCoffee\settings.txt";
string settings = File.ReadAllText(filePath);
bool overwrite = true;
File.Copy(sourceSettingsPath, destinationSettingsPath, overWrite);
FileInfo - new로 접근 객체 생성 시에만 security
string filePath = @"C:\fourthCoffee\settings.txt";
FileInfo settings = new FileInfo(filePath);
bool overwrite = true;
FileInfo settings = new FileInfo(sourceSettingsPath);
settings.CopyTo(destinationSettingsPath, overwrite);
Serializing and Deserializing Data
serialize : 데이터를 통째로 저장
[Serializable]
public class ServiceConfiguration
{
...
}
특정 개체만 저장을 안하려면 [NonSerialized]
[Serializable]
public class ServiceConfiguration : ISerializable
{
[NonSerialized]
private Guid _internalId;
public string ConfigName { get; set; }
public string DatabaseHostName { get; set; }
public string ApplicationDataPath { get; set; }
public ServiceConfiguration()
{
}
public ServiceConfiguration(SerializationInfo info, StreamingContext ctxt)
{
this.ConfigName
= info.GetValue("ConfigName", typeof(string)).ToString();
this.DatabaseHostName
= info.GetValue("DatabaseHostName", typeof(string)).ToString();
this.ApplicationDataPath
= info.GetValue("ApplicationDataPath", typeof(string)).ToString();
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("ConfigName", this.ConfigName);
info.AddValue("DatabaseHostName", this.DatabaseHostName);
info.AddValue("ApplicationDataPath", this.ApplicationDataPath);
}
}
serialize한 데이터를 읽어오기 Deserilze
// Create the formatter you want to use to serialize the object.
IFormatter formatter = new BinaryFormatter();
// Create the stream that the serialized data will be buffered too.
FileStream buffer = File.OpenRead(@"C:\fourthcoffee\config.txt");
// Invoke the Deserialize method.
ServiceConfiguration config = formatter.Deserialize(buffer) as ServiceConfiguration;
// Close the stream.
buffer.Close();
soap, binary, json
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
//참조추가 System.Runtime.Serialization.Formatters.Soap.dll
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;//FileStream
using System.Runtime.Serialization.Formatters.Binary;
//참조추가 System.Runtime.Serialization.dll
//NuGet패키지 관리자로 설치
using Newtonsoft.Json;
namespace Mod6Serialization
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//참조추가 System.Runtime.Serialization.Formatters.Soap.dll
private void button1_Click(object sender, EventArgs e)
{
Config c = new Config("127.0.0.1", 80, "MyDBServer");
SoapFormatter sf = new SoapFormatter();
FileStream f = new FileStream("config.soap", FileMode.Create);
sf.Serialize(f, c); //<====
f.Close();
}
private void button2_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("config.soap", FileMode.Open);
SoapFormatter sf = new SoapFormatter();
Config c = (Config)sf.Deserialize(f);//<===
listBox1.Items.Add(c.IP);
listBox1.Items.Add(c.Port);
listBox1.Items.Add(c.DBServer);
f.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Config c = new Config("127.0.0.1", 80, "MyDBServer");
BinaryFormatter f = new BinaryFormatter();
FileStream fs = new FileStream("config.bin", FileMode.Create);
f.Serialize(fs, c);
fs.Close();
}
private void button4_Click(object sender, EventArgs e)
{
BinaryFormatter f = new BinaryFormatter();
FileStream fs = new FileStream("config.bin", FileMode.Open);
Config c = f.Deserialize(fs) as Config;
listBox1.Items.Add(c.IP);
listBox1.Items.Add(c.Port);
listBox1.Items.Add(c.DBServer);
fs.Close();
}
//참조추가 System.Runtime.Serialization.dll
private void button5_Click(object sender, EventArgs e)
{
Config c = new Config("127.0.0.1", 80, "MyDBServer");
DataContractJsonSerializer json = new DataContractJsonSerializer(c.GetType());
FileStream fs = new FileStream("config.json", FileMode.Create);
json.WriteObject(fs, c);
fs.Close();
}
private void button6_Click(object sender, EventArgs e)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Config));
FileStream fs = new FileStream("config.json", FileMode.Open);
Config c = json.ReadObject(fs) as Config;
listBox1.Items.Add(c.IP);
listBox1.Items.Add(c.Port);
listBox1.Items.Add(c.DBServer);
fs.Close();
}
// NuGet패키지 관리자로 설치
private void button8_Click(object sender, EventArgs e)
{
Config2 c = new Config2("127.0.0.1", 80, "MyDBServer");
//Object=> JSON string으로 변환
string s = JsonConvert.SerializeObject(c);
listBox1.Items.Add(s);
//object-> File로 저장
StreamWriter w = new StreamWriter("Config2.json");
JsonSerializer json = new JsonSerializer();
json.Serialize(w, c);
w.Close();
}
private void button7_Click(object sender, EventArgs e)
{
JsonSerializer json = new JsonSerializer();
StreamReader sr = new StreamReader("config.json");
JsonReader r = new JsonTextReader(sr);
Config2 c = json.Deserialize<Config2>(r) as Config2;
listBox1.Items.Add(c.IP);
listBox1.Items.Add(c.Port);
listBox1.Items.Add(c.DBServer);
sr.Close();
}
}
}
buffer
default = UTF-8
Encoding.Default : ANSI encoding
DB 저장 방법
1. ADO .NET Calsses : DataSet OR Data
System.Data
System.Data.SqlClient // MS SQL server
System.Data.OracleClient
Syetem.Data.Oledb
Connection - SqlConnection,
OracleConnection
OleDbConnection
2. ADO .NET EF(Entity Framework) -VS2008
- Entity Data Model Wizard
<-- LINQ to Entity
DB 서버 : (localdb)\MSSQLLocalDB
인증 : Windows인증
App.config 에서 인증 문자열을 저장하고 가져와야 함
Querying Data by Using LINQ
저장 프로시저
암호화해서 배포하고 코드 수정할 땐 복호화
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config 기본 암호화 config 위치
//암호화
//using System.Configuration; // System.Configuration.dll 참조 추가
private void button10_Click(object sender, EventArgs e)
{
//ConnectionString암호화
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.ConnectionStrings;
if (section != null)
{
if (!section.SectionInformation.IsProtected && !section.ElementInformation.IsLocked)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
}
private void button11_Click(object sender, EventArgs e)
{
//ConnectionString 복호화
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.ConnectionStrings;
if (section != null)
{
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceDeclaration(true);
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
}
데이터가 많아지면 페이징해서 가져와야함
WPF
-화려한 UI, Animation구현
tips
도구 > GUID만들기
LINQ 101 Samples
LINQSamples | LINQ 101 Query and Lambda Expression Samples
© 2023 - LINQSamples.com | Terms Of Service - Privacy Policy
linqsamples.com
솔루션 우클릭> 시작프로젝트 설정>여러개의 시작프로젝트 지정