0

我对 c# 完全陌生。我创建了一个小脚本,可以在 Visual Studio 中执行,但无法从终端编译。这是我的代码:

using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;

namespace Examples.System.Net
{public class WebRequestGetExample{

    public static void Main()
    {
        //Create request
        WebRequest request = WebRequest.Create(
          "whatever_http_address");

        // Set the credentials  
        request.Credentials = new NetworkCredential("username", "password");
        request.PreAuthenticate = true;

        // Get the response  
        WebResponse response = request.GetResponse();
        // //capture json response as a string named json
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

            string json = reader.ReadToEnd();
            Console.WriteLine(json);
            //Parse json
            JObject jo = JObject.Parse(json);
            //Extract attachment
            string excel_64 = (string)jo["attachment"];
            //Remove unnecessary string snippet
            string replacedString = excel_64.Replace("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", "");
            //Decode from base64 to bytes
            byte[] textAsBytes = Convert.FromBase64String(replacedString);
            //Create binary writer
            BinaryWriter bw = new BinaryWriter(new FileStream("/path/to/my/excel_file", FileMode.Create));
            //Write to file
            bw.Write(textAsBytes);
        }
    }
}
}

我可以在 Visual Studio 中正确执行它,但是当我运行时:

csc Program.cs

我得到:

Program.cs(5,7): error CS0246: The type or namespace name 'Newtonsoft' could 
not be found (are you missing a using
directive or an assembly reference?)

我已将文件 Newtonsoft.Json.dll 下载到 Program.cs 所在的同一文件夹中,当我运行时:

csc ./reference:Newtonsoft.Json.dll Program.cs

我得到:

错误 CS1504:无法打开源文件 C:\Users\jramirezle001\source\repos\ConsoleApp1\ConsoleApp1./reference:Newtonsoft.Json.dll' -- 不支持给定路径的格式。

此外,如果我运行:

csc Newtonsoft.Json.dll Program.cs

我得到:

错误 CS2015:'C:\Users\jramirezle001\source\repos\ConsoleApp1\ConsoleApp1\Newtonsoft.Json.dll' 是二进制文件而不是文本文件

所以,我完全迷失了。任何帮助将非常感激!先感谢您!

4

0 回答 0