# C#
## Connect C# to API
Add `using System.Net.Http;`
```cpp
static async Task<String> run(string ac,string pw)
{
Uri uri = new Uri("http://192.168.88.143:90/api/login/" + ac + "/" + pw);
HttpClient client = new HttpClient();
client.BaseAddress = uri;
using (HttpResponseMessage respone = await client.GetAsync(uri))
using (HttpContent content = respone.Content)
{
return respone.StatusCode.ToString();
//return await content.ReadAsStringAsync();
}
}
private async void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = await run(textBox1.Text,textBox2.Text);
}
```
## WebBrowser
Set data source
`string str = (@"file://" + System.Environment.CurrentDirectory +@"/_.jpg");`
<sub> System.Environment.CurrentDirectory => file\bin\Debug</sub>
Get data
`webBrowser1.Navigate(str);`
## Open Document(picture)
`using System.Diagnostics;`
`Process.Start(@"file://" + System.Environment.CurrentDirectory + @"/_.jpg");`
<sub> System.Environment.CurrentDirectory => file\bin\Debug</sub>
## Connect to Excel
```cpp
using excel = Microsoft.Office.Interop.Excel;
```
```cpp
excel.Application application = new excel.Application();
excel.Workbooks workbooks = application.Workbooks;
excel.Workbook workbook = workbooks.Add();
excel.Worksheet worksheet = workbook.ActiveSheet;
application.Visible = true;
worksheet.Name = "Statistics Report";
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = dataGridView2.Columns[i].Name;
for (int j = 0; j < dataGridView2.RowCount; j++)
{
try
{
worksheet.Cells[j + 2, i + 1] = dataGridView2.Rows[j].Cells[i].Value.ToString();
}
catch (NullReferenceException)
{
worksheet.Cells[j + 1, i + 1] = "";
}
}
}
workbook.SaveAs(System.Environment.CurrentDirectory + "\\report.xlsx");
application.Quit();
MessageBox.Show("path:" + System.Environment.CurrentDirectory.ToString() + "\\report.xlsx", "Excel peport save sucess");
```
## Datagridview get click item
```cpp
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int No = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
var list = from a in db.Attractions
where a.ThemeParkID == No
select a;
dataGridView1.DataSource = list.ToList();
}
```
## Tabcontrol
```cpp
this.tabPage1.Parent = this.tabControl;//顯示
this.tabPage2.Parent = null;//隱藏
this.tabPage3.Parent = null;//隱藏
```
## Word Ticket general
`using word = Microsoft.Office.Interop.Word;`
```cpp
{
string str = (@"file://" + System.Environment.CurrentDirectory + @"/3570970_orig.jpg");
string str2 = (@"file://" + System.Environment.CurrentDirectory + @"/a.png");
string str3 = System.Environment.CurrentDirectory + @"/a.pdf";
webBrowser1.Navigate(str);
Bitmap a = BarcodeService.QRCode.Generate(str, 100, 100);
a.Save(System.Environment.CurrentDirectory + @"/a.jpg");
word.Application wordss = new word.Application();
word.Document document = wordss.Documents.Add();
//document.InlineShapes.AddPicture(str2);
word.Table table = document.Tables.Add(wordss.Selection.Range, 2, 3);
table.Borders.Enable = 1;
table.Cell(1, 1).Range.Text = "(1, 1)";
table.Cell(1, 2).Range.Text = "(1, 2)";
table.Cell(1, 3).Range.Text = "(1, 3)";
table.Cell(2, 1).Range.Text = "(2, 1)";
table.Cell(2, 2).Range.Text = "(2, 2)";
table.Cell(2, 3).Range.Text = "(2, 3)";
for (int i = 1; i < 3; i++)
{
table.Cell(i, 1).Height = 100;
table.Cell(i, 1).Range.Text = name[i - 1] + "\n" + day[i - 1] + "\n" + table.Cell(i, 1).Range.Text;
(QRCode.Generate(table.Cell(i, 1).Range.Text, 100, 100)).Save(System.Environment.CurrentDirectory + @"/a.png");
table.Cell(i, 2).Range.InlineShapes.AddPicture(str2);
table.Cell(i, 2).Width = 100;
table.Cell(i, 3).Range.InlineShapes.AddPicture(str);
}
table.Cell(1, 1).Range.Font.Shading.ForegroundPatternColor = word.WdColor.wdColorBlue;
document.SaveAs2(str3, word.WdSaveFormat.wdFormatPDF);
}
```
## API Setting
```cpp
[Route("api/books")]
[HttpPost]
```
## XmlDocument
`Namaspace:System.Xml`
```cpp
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);//xmlstring 是傳入 XML 格式的 string
doc.GetElementsByTagName("result")[0]?.InnerText;//取得第一個 result 節點內的值
```