Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Remove the extension from a file name in C#
You can use Path.GetFileNameWithoutExtension: DirectoryInfo dirInfo = new DirectoryInfo(currentDirName); FileInfo[] smFiles = dirInfo.GetFiles("*.txt"); foreach (FileInfo file in smFiles) { builder.Append(Path.GetFileNameWithoutExtension(file.Name)); builder.Append(", "); }
You can use Path.GetFileNameWithoutExtension:
DirectoryInfo dirInfo = new DirectoryInfo(currentDirName);
FileInfo[] smFiles = dirInfo.GetFiles(“*.txt”);
foreach (FileInfo file in smFiles)
See less{
builder.Append(Path.GetFileNameWithoutExtension(file.Name));
builder.Append(“, “);
}
How to get last 4 characters from string in C#?
use the substring() method to get the last 4 characters str.Substring(str.Length - 4);
use the substring() method to get the last 4 characters
str.Substring(str.Length – 4);
See lessHow can update the values of a collection using LINQ in ASP.NET?
We can make use of ForEach extension method which is available as part of LINQ. collection.ToList().ForEach(o => o.PropertyToSet = value); The ToList is needed in order to evaluate the select immediately due to lazy evaluation
We can make use of ForEach extension method which is available as part of LINQ.
collection.ToList().ForEach(o => o.PropertyToSet = value);
The ToList is needed in order to evaluate the select immediately due to lazy evaluation
See lessNo migrations configuration type was found in the assembly
After you run Enable-Migrations and the Configuration file is created, rebuild the project and run Enable-Migrations -Force again.
After you run Enable-Migrations and the Configuration file is created, rebuild the project and run
See lessEnable-Migrations -Force again.
How to convert bytes to KB,MB, GB in .NET ?
using Log to solve the problem static String BytesToString(long byteCount) { string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB if (byteCount == 0) return "0" + suf[0]; long bytes = Math.Abs(byteCount); int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)))Read more
using Log to solve the problem
static String BytesToString(long byteCount)
{
string[] suf = { “B”, “KB”, “MB”, “GB”, “TB”, “PB”, “EB” }; //Longs run out around EB
if (byteCount == 0)
return “0” + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
}
Another solution
This may not the most efficient way to do it, but it’s easier to read if you are not familiar with log maths, and should be fast enough for most scenarios.
string[] sizes = { “B”, “KB”, “MB”, “GB”, “TB” };
double len = new FileInfo(filename).Length;
int order = 0;
while (len >= 1024 && order < sizes.Length – 1) {
order++;
len = len/1024;
}
// Adjust the format string to your preferences. For example “{0:0.#}{1}” would
// show a single decimal place, and no space.
string result = String.Format(“{0:0.##} {1}”, len, sizes[order]);
See lessHow to make Swagger Codegen work with .NET core
to integrate swagger codegen into your web api project you have to install "Swashbuckle.AspNetCore.Swagger" nuget package by following the steps below : right click your project and click manage nuget packages and install "Swashbuckle.AspNetCore.Swagger" nuget package OR right click in dependenciesRead more
to integrate swagger codegen into your web api project you have to install “Swashbuckle.AspNetCore.Swagger” nuget package by following the steps below :
right click your project and click manage nuget packages and install “Swashbuckle.AspNetCore.Swagger” nuget package
OR
right click in dependencies and click manage nuget packages and install “Swashbuckle.AspNetCore.Swagger” nuget package
Then add the following code into your project startup class
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(“v1”, new OpenApiInfo { Title = “API”, Version = “v1” });
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint(“/swagger/v1/swagger.json”, “API v1”));
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
How get wwwroot folder path from ASP.NET core
Step 1: You will need to import the following namespace. using Microsoft.AspNetCore.Hosting; Step 2: By Using IHostingEnvironment public class HomeController : Controller { private IHostingEnvironment Environment; public HomeController(IHostingEnvironment _environment) { Environment = _environment;Read more
Step 1:
You will need to import the following namespace.
using Microsoft.AspNetCore.Hosting;
Step 2:
By Using IHostingEnvironment
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
return View();
}
}
By Using IWebHostEnvironment
See lessIn the below example, the IWebHostEnvironment is injected in the Controller and assigned to the private property Environment and later used to get the WebRootPath and ContentRootPath.
public class HomeController : Controller
{
private IWebHostEnvironment Environment;
public HomeController(IWebHostEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
return View();
}
}
What does & lt; and & gt; refer to?
< stands for the less-than sign:
< stands for the less-than sign:
See lessHow to Import or include a JavaScript file inside another JavaScript file?
Earlier javascript was not having any feature to import and export javascript but since the discovery of ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes, or any other entity to/from other JS files. Suppose we havRead more
Earlier javascript was not having any feature to import and export javascript but since the discovery of ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes, or any other entity to/from other JS files.
Suppose we have two files named script.js and app.js and you want to export variables and functions script.js to app.js Then in the “script.js” file you need to write the export statement as shown in the following example:
Example :
let message = “Hi How are you doing?”;
const PI = 3.14;
function addNumbers(a, b){
return a + b;
}
// Exporting variables and functions
export { message, PI, addNumbers };
And in the “app.js” file you need to write the import statement as shown below:
import { message, PI, addNumbers } from ‘./script.js’;
console.log(message); // Prints to the console : Hi How are you doing?
See lessconsole.log(PI); // Prints to the console : 3.14
console.log(addNumbers(10, 10)); // Prints to the console : 20
How to get input in java?
In Java the Scanner class allows the user to take input from the console. It belongs to the java.util package.It is used to read the input of primitive types like int, double, long, short, float, and byte. You can use this example below to understand the way the Scanner class helps to take input froRead more
In Java the Scanner class allows the user to take input from the console. It belongs to the java.util package.It is used to read the input of primitive types like int, double, long, short, float, and byte.
You can use this example below to understand the way the Scanner class helps to take input from the user
import java.util.*;
See lessclass UserInputDemo1
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print(“Enter your name: “);
String str= sc.nextLine(); //reads string
System.out.print(“Your name is : “+str);
}
}
Output :
Your name is : Sourav //In this case I have taken Input as Sourav so it is printing in the console as Your name is : Sourav