在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;