在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;
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:在C#中如何调用PHP脚本