C
C#4d ago
Faker

✅ App.config file and .env file

Hello guys, this doesn't concern only C# but I think it's in general. What is the difference between a .config file and .env file? Both stores sensitive data no? For example, our database connection string can be stored in either one. Which one should I choose and why. Second thing... when we create a solution in C#, does it automatically create an App.config why? if no, how to proceed to create one please.
18 Replies
UINT256
UINT2564d ago
In C#, it's generally best practice to store your database connection string in the appsettings.json file (for .NET Core applications) or in the Web.config/App.config file (for older ASP.NET applications). These configuration files are specifically designed to hold application settings, including connection strings, making it easier to manage configurations and read them at runtime. Storing connection strings in a traditional configuration file keeps them contained within the application context, which facilitates easier deployment and version control. Using a .env file might be suitable for sensitive information in environments where configuration management tools are employed, especially in containerized applications (like with Docker), allowing for greater flexibility across different environments (development, staging, production). However, the standard configuration files provide built-in support for encryption and other security features that may be necessary for protecting sensitive data, making them a more straightforward choice in many scenarios. Second problem: No, a C# solution in Visual Studio does not automatically create an App.config file by default for all project types. Here's how to create an App.config file if it's not already present: -Right-click on your project in Solution Explorer: In Visual Studio, locate your project in the Solution Explorer (usually on the right side of the IDE). Right-click on the project name. -Add a New Item: From the context menu, select "Add" -> "New Item...". -Choose "Application Configuration File": In the "Add New Item" dialog box, search for "Application Configuration File" or "App.config." Select this item. -Name the File (Optional): By default, Visual Studio will name the file App.config. You can change the name if you like, but keeping the default is recommended for consistency. -Click "Add": Click the "Add" button to create the App.config file in your project. This is the answer of gpt. You can use gpt for something like the problems.
Faker
FakerOP4d ago
I did use chatGPT xd but it was still vague so I wanted other suggestions
UINT256
UINT2564d ago
My answer is still vague?
Faker
FakerOP4d ago
This is the answer of chatGPT, I wanted something else
UINT256
UINT2564d ago
I think we can insert the database connection string into both files, but I think each has its own role. I think the reason is similar to configuring each component separately in React. Project should be eye-catching and easy to look at, although I don't think that's all that important here.
Pobiega
Pobiega4d ago
App.config only exists for .NET Framework projects, or if you are using ConfigurationManager (which is originally from .NET Framework and has been fully replaced by the significantly better M.E.Configuration library). .env files is not a .NET thing, and the idiomatic solution is to use user secrets instead. For configuration, sensitive data or no, use Microsoft.Extensions.Configuration with user secrets.
UINT256
UINT2564d ago
I think so.
Pobiega
Pobiega4d ago
I wasn't asking :p Also, please don't use GPT to answer peoples questions here, its better to just not answer in that case.
UINT256
UINT2564d ago
I see
leowest
leowest4d ago
please refer to the #info-and-rules
Relaying ChatGPT/AI generated answers or sending Let Me Google That For You links is heavily discouraged
UINT256
UINT2564d ago
I am sorry.
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
sorry for late reply, we no longer use config or env files? I'm just building a console app where I want to connect with SQL server; I have successfully connected with it but my connection string is in the program itself, I think this is wrong practice... I should use the appsettings.json or appsettings.{env}.json as @TeBeCo mentioned But hmm what's the difference betwen the appsettings.json and the appsettings.{env}.json pls
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
:c, wait will do it straight away
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
thanks, will have a look 2sec
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?