J
JOSE MANUEL RAMIREZ LEON
Guest
Hi!
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!
Continue reading...
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!
Continue reading...