在C#腳本中可以通過幾種方式調用外部資源,其中常見的方式包括:
using System.Net;
WebClient client = new WebClient();
string result = client.DownloadString("http://www.example.com/api/data");
using System.IO;
string filePath = @"C:\example.txt";
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
// 讀取文件內容
}
// 或者寫入文件內容
using (FileStream fs = new FileStream(filePath, FileMode.Append))
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteLine("Hello, World!");
}
}
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern bool MessageBox(IntPtr hWnd, string text, string caption, uint type);
MessageBox(IntPtr.Zero, "Hello, World!", "Message", 0);
using Newtonsoft.Json;
string json = "{\"name\":\"John\", \"age\":30}";
dynamic data = JsonConvert.DeserializeObject(json);
string name = data.name;
int age = data.age;