Home > front end >  Hide Json file containing GOOGLE_APPLICATION_CREDENTIALS when building executable file in Visualstud
Hide Json file containing GOOGLE_APPLICATION_CREDENTIALS when building executable file in Visualstud

Time:12-31

currently I am developing a tool that interacts with a Firebase Firestore database. When I want to make the C# Forms Application an executable file I get the .exe but also the json file which contains the Google App Credentials. However, I want to forward the tool so that you can't see the json file or read the contents of the file, so you only need the .exe file. Is there a way to achieve this? For example, define the app credentials in a C# script so that it compiles to the .exe file? If so how?

My current implementation looks like this:

string path = AppDomain.CurrentDomain.BaseDirectory   @"cloudfire.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);

The cloudfire.json file is directly contained in the namespace "LUX".

I also tried making the cloudfire.json file a resource, since i read this post but then the problem is, that i can't set the path of the .json, if i try it like that:

var assembly = Assembly.GetExecutingAssembly();
            string resourceName = assembly.GetManifestResourceNames()
  .Single(str => str.EndsWith("cloudfire.json"));

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", resourceName);

I get the error: System.InvalidOperationException: "Sequence contains no matching element"

Is there maybe a way to set the "GOOGLE_APPLICATION_CREDENTIALS" to the embedded cloudfire.json ressource file?

EDIT: I solved the problem by adding the "cloudfire.json" file to Resources.resx and changed the modifier to public. Like mentioned here. Since you can only set the GOOGLE_APPLICATION_CREDENTIALS by using this code:


Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "path to file");

I solved it by creating a temporary file:

byte[] resourceBytes = Properties.Resources.cloudfire;

// Write the resource to a temporary file
string tempPath = Path.GetTempFileName();
File.WriteAllBytes(tempPath, resourceBytes);
            
// Set the GOOGLE_APPLICATION_CREDENTIALS environment variable
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", tempPath);

CodePudding user response:

Add you file as embedded resource with name. And try to read by following code:

var resources = new ResourceManager("<namespace>", Assembly.GetExecutingAssembly());
var obj = resources.GetObject(<embedded_resource_key>); 
or 
var str = resources.GetString(<embedded_resource_key>)
  • Related