@manhnguyenv

Welcome to my blog!

Folder Path

April 16, 2020 17:02

Folder Path in C# (edit)

FolderPath.WebApplication

RuntimeEnvironment.GetSystemVersion() + Temporary ASP.NET Files C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\Log_20200416.txt
System.Environment.SpecialFolder.CommonApplicationData C:\ProgramData\Log_20200416.txt
System.Environment.SpecialFolder.ApplicationData C:\Users\nvmanh\AppData\Roaming\Log_20200416.txt
System.Environment.SpecialFolder.LocalApplicationData C:\Users\nvmanh\AppData\Local\Log_20200416.txt
System.AppDomain.CurrentDomain.BaseDirectory C:\Users\nvmanh\source\repos\FolderPath.WebApplication\\Log_20200416.txt

 public ActionResult Index()
{
string net_base = System.IO.Path.GetFullPath(System.IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), @"..\.."));
string strTemp32 = string.Concat(net_base, @"\Framework\", System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion(), @"\Temporary ASP.NET Files");
string strTemp64 = string.Concat(net_base, @"\Framework64\", System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion(), @"\Temporary ASP.NET Files");

ViewBag.TmpFilePath = strTemp32
+ @"\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

ViewBag.TmpFilePath2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)
+ @"\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

ViewBag.TmpFilePath1 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)
+ @"\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

ViewBag.TmpFilePath3 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)
+ @"\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

ViewBag.TmpFilePath4 = System.AppDomain.CurrentDomain.BaseDirectory
+ @"\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

return View();
}

FolderPath.WinForm + FolderPath.ConsoleApplication

using System;

namespace FolderPath.ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location): ");
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
Console.WriteLine();

Console.WriteLine("System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location): ");
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
Console.WriteLine();

Console.WriteLine("System.AppDomain.CurrentDomain.BaseDirectory: ");
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine();

Console.WriteLine("System.Environment.CurrentDirectory: ");
Console.WriteLine(System.Environment.CurrentDirectory);
Console.WriteLine();

Console.WriteLine("System.IO.Directory.GetCurrentDirectory(): ");
Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
Console.WriteLine();

Console.WriteLine("System.Environment.CurrentDirectory: ");
Console.WriteLine(System.Environment.CurrentDirectory);
Console.WriteLine();

Console.WriteLine("System.Windows.Forms.Application.StartupPath: ");
Console.WriteLine(System.Windows.Forms.Application.StartupPath);
Console.WriteLine();

Console.WriteLine("System.IO.Path.GetDirectoryName(System.Environment.GetCommandLineArgs()[0]): ");
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Environment.GetCommandLineArgs()[0]));
Console.WriteLine();

Console.WriteLine("System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath): ");
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));

Console.WriteLine();
}
}
}

Categories

Recent posts