0

I am completely new to c#. I have created a small script, which I can execute in Visual Studio, but which I cannot compile from terminal. Here is my code:

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);
        }
    }
}
}

I can execute it correctly in Visual Studio, but when I run:

csc Program.cs

I get:

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?)

I have downloaded the file Newtonsoft.Json.dll to the same folder where Program.cs is, and when I run:

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

I get:

error CS1504: Source file C:\Users\jramirezle001\source\repos\ConsoleApp1\ConsoleApp1./reference:Newtonsoft.Json.dll' could not be opened -- The given path's format is not supported.

Further, if I run:

csc Newtonsoft.Json.dll Program.cs

I get:

error CS2015: 'C:\Users\jramirezle001\source\repos\ConsoleApp1\ConsoleApp1\Newtonsoft.Json.dll' is a binary file instead of a text file

So, I am completely lost. Any help would be much appreciated! Thank you in advance!

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • The question shows the format you should be using. You may want to check the documentation for `csc`: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/reference-compiler-option – Camilo Terevinto Dec 10 '18 at 17:14
  • Let me ask a silly question: Why do you want to use `csc` directly? I worked as a C# developer for a decade and can count the number of times I had to compile something with `csc` on zero hands. – Daniel Mann Dec 10 '18 at 17:15
  • Hi Daniel, the thing is I am not a c# developer, I have been asked to write this in c# because "The client cannot maintain code not written in c#". I would like to know if I can use csc since I will probably have to insert this code in a virtual machine (vm), whose OS I do not even know, and thought csc would be easier to find in such vm. Thank you really for any insight into my problem! – JOSE MANUEL RAMIREZ LEON Dec 10 '18 at 17:23
  • 1
    It is /reference, not ./reference. Use an IDE to avoid silly mistakes like this. – Hans Passant Dec 10 '18 at 17:32
  • 1
    Thank you, Camilo and Hans, question answered, I got to generate the exe file :) – JOSE MANUEL RAMIREZ LEON Dec 10 '18 at 17:33

0 Answers0