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();
}
}