📜  获取文档路径 c# - TypeScript (1)

📅  最后修改于: 2023-12-03 15:27:50.650000             🧑  作者: Mango

获取文档路径 - C# & TypeScript

在编程中,获取文件路径是一个常见需求。在C#和TypeScript中,我们可以使用以下方法来获取文件路径。

C#
当前应用程序域的基础目录

可以使用AppDomain.CurrentDomain.BaseDirectory来获取当前应用程序域的基础目录。

string path = AppDomain.CurrentDomain.BaseDirectory;
当前工作目录

可以使用Environment.CurrentDirectory来获取当前工作目录。

string path = Environment.CurrentDirectory;
程序集的位置

可以使用Assembly.GetExecutingAssembly().Location来获取正在执行的程序集的位置。

string path = Assembly.GetExecutingAssembly().Location;
特定文件的位置

可以使用Path.CombineAssembly.GetExecutingAssembly().Location来获取特定文件的位置。

string fileName = "file.txt";
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fileName);
TypeScript
当前页面的URL

可以使用window.location.href来获取当前页面的URL。

let path: string = window.location.href;
当前页面的主机名

可以使用window.location.hostname来获取当前页面的主机名。

let path: string = window.location.hostname;
当前页面的路径名

可以使用window.location.pathname来获取当前页面的路径名。

let path: string = window.location.pathname;
特定文件的位置

可以使用相对路径或绝对路径来获取特定文件的位置。

let fileName: string = "file.txt";
let path: string = "./" + fileName;