尝试通过csc编译时出现错误CS0246,CS1504和CS2015

时间:2018-12-10 17:11:37

标签: c# csc

我对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'是二进制文件而不是文本文件

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

0 个答案:

没有答案