Xamarin - System.IO.File - namespace 'File' does not exist - c#

I am trying to load a local json file into a string such that I can parse/deserialize it into an object.
However for some reason despite all the examples and the Xamarin support documentation suggesting I can use System.IO.File.ReadAllText(...) I get the following error:
c# error: 'File' type or namespace name not found
My code seems pretty simple, I can't get past loading the file, which seems like it should be so simple!
string jsonInput = System.IO.File.ReadAllText("example.json", Encoding.UTF8);
Quiz temp = JsonConvert.DeserializeObject<Quiz>(jsonInput);
My header includes System.IO amongst the rest too..
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel; //ObservableCollection
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;`
I've also tried the following solution:
var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("example.json");
string jsonInput = "";
using (var reader = new System.IO.StreamReader(stream))
{
jsonInput = reader.ReadToEnd();
}
However in this case 'LoadResourceText' is not found and I can't work out what type should replace it.
Any help or suggestions would be greatly appreciated!
My first post on StackOverflow, hope I did it right..

If you are using this file as a local resource, make sure that it is available for each device, for example, in the Android project, select the file/properties/advanced/build action and set it to embedded resource.
Sometimes the code is fine, it's just that the file 'doesn't exist' for the device

^See above response as well^
I have solved this myself.
I found my answer hidden in a forum post by YkshLeo on the Xamarin Forums:
"...make sure the Build Action for your resource is set to "Embedded Resource"
I really wish Xamarin would make their documentation a little more thorough. Maybe it's my .Net noobness coming through.

Related

what reference I need to add in C# to get LaunchPadApi?

I try to connect to the API (Using C#) that client provided, but it is using LaunchPadApi() I am not sure what assembly I need to add to get this, these are 2 references that were added:
using IO.Swagger.Api;
using IO.Swagger.Client;
I was able to find using IO.Swagger.Client; but I am not able to find using IO.Swagger.Api;
This is a sample code I am not sure how I can get LaunchPadApi defined so "responsePost" method is not recognized as well,
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
public class responsePostExample
{
public void main()
{
var apiInstance = new LaunchPadApi();
....
try
{
apiInstance.responsePost(body, xAPIKey);
}
....
Install Launchpad in your nuget package manager for the project
Add using Launchpad; to the top of your file

Visual Studio / RestSharp - Cannot navigate to the symbol under the caret (CS0118)

I have only been coding for about two years now (off and on) but I'm well acquainted with this cryptic error across multiple versions of VS. I know this issue isn't related to just RestSharp.
This 'caret error' has been the bane of my VS experience so that is why I'm here.
Environment:
Microsoft Visual Studio Community 2019 Version 16.8.1
RestClient v4.0.30319
Windows 10 Professional
What I'm trying to do:
I'm following the very simple directions on the RestClient site. I have installed RestClient, added references but when I try to get started I get that dreaded caret error.
None of this is my code aside from the reference to RestClient, which may or may not be appropriate (I don't know).
Screen Snip
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;
namespace newDawn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var client = new RestClient("https://api.twitter.com/1.1");
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);
var response = client.Get(request);
}
}
}
Thanks for reading.
using RestClient;
Here you are declaring that you are using the namespace RestClient, not the object.
var client = new RestClient(...
The error is raised here because you are trying to create an instance of the namespace you just declared you were using.
It's uncommon and confusing for an object to share a name with part of its namespace, so you should generally not expect to see a using statement with the same name you use to create an object.
Namespaces are used to separate, organize classes and limit what classes are loaded at compile time.
Classes of the same name can exist in different namespaces.
You could create your own class named RestClient and use it in this project as well(maybe for testing) as long as they existed in different namespaces. The full declaration of the class you are using now is RestSharp.RestClient. You could create a class with the full declaration Facundo.Test.RestClient and use them both in the same project. However, in this case, you could not use using statements and everytime you wanted to instantiate one or the other you would need to fully declare the namespace
RestSharp.RestClient client = new RestSharp.RestClient(...);
or
Facundo.Test.RestClient testClient = new Facundo.Test.RestClient(...);
I would not recommend naming them the same in practice, just trying to illustrate namespaces.

Utils.GetFileInfo error

I'm going through the sample projects of EPPlus and finally reached the part that I'm interested in. I would like to import contents of a .txt file. Sample 9 shows how to do this, but I get an error I can't seem to fix.
The error message:
The type or namespace name "GetFileInfo" doesn't exist in the namespace 'OfficeOpenXml.Utils'(are you missing an assembly reference?)
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeOpenXml;
using System.IO;
using OfficeOpenXml.Table;
using OfficeOpenXml.Drawing.Chart;
using System.Globalization
...
FileInfo newFile = Utils.GetFileInfo(#"\sample9.xlsx");
Does anyone encountered this issue? Thanks in advance!
Edit: If I open directly the sample file I don't get any errors, only if I try to do it in my own project.
Try this:
var file = new System.IO.FileInfo(fullpathandfilename);

Error deserializing JSON credential data. Google Cloud File Upload C# Asp.Net

I'm trying to establish connection to google cloud for media upload
I've used various methods like Setting environment variable, saving json to mongodb , tried directly load json from file and then loading to google functions
libraries:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DAL;
using MongoDB;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using System.Drawing;
using Newtonsoft.Json;
using MongoDB;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
Method 1: Using Environment Variable.
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS","onfer-gc.json");
var Credential1 = GoogleCredential.GetApplicationDefaultAsync();
Error : Using Environment Variable.
Error reading credential file from location onfer-gc.json: Error deserializing JSON credential data.
Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS
Method 2: Load json from file
FileStream stream1 = File.OpenRead("onfer-gc.json");
var Credential = GoogleCredential.FromStream(stream1);
Error deserializing JSON credential data.
Method 3: Loading data from Mongodb database and then trying to turn into string
var Db = new Database();
var result = Db.GetCollection<company>().FindOne(Query.EQ("type", "service_account"));
var Credential = GoogleCredential.FromJson(result);
In this case it gives error
Element 'type' does not match any field or property of class MongoDB.company.
Even I've checked solutions online
https://github.com/google/google-api-dotnet-client/issues/747
But it didn't work. What am i doing wrong here ?
My case may not answer the question directly, but it might be possible cause for such error. In my projects I have references to two different versions of Newtonsoft.json, one is 12.0.2 and the other one is 7.0.1. Once I had 7.0.1 updated to same 12.0.2, the json deserializing error solved. This might explain some random working/not-working situations.
I was successfull establishing connection using Method 2:
string credPath = "D:\\Admin\\gcloudcred.json";
var credential = GoogleCredential.FromStream(File.OpenRead(credPath));
var storageClient = StorageClient.Create(credential);
listvideos = storageClient.ListObjects("bucketname");
if (listvideos == null)
{
return listvideos = listvideos1;
}
return listvideos;

"Name 'Process' does not exist in current context" error in console app

I'm trying to use the Process class but it gives me this compiler error every time I do
The name 'Process' does not exist in the current context
I've already added using System.Diagnostics; at the top of my code but I still get this error. Autocomplete wont even recognize the class.
Hear is the code. I've only included the Main method because the rest of the code has nothing to do with the error.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
public class MainClass
{
static void Main()
{
AIRuntime adam = new AIRuntime("Adam", false);
adam.AIUpdate();
Process.Start(adam.directory); //adam.directory is just a string
}
}
My IDE is Xamarin Studio if it matters.
After web searching and attempts, I found the problem.
To fix this, I manually add a reference to System in my project options instead of just typing using System;. I also need to keep using System.Diagnostics; at the top. It's all in the picture below
Thanks guys for trying to help

Categories

Resources