[英]How to set file path to the root of the current directory
I want to save my log file to the root of the current directory. 我想将日志文件保存到当前目录的根目录。
Right now my file path is "path": "%temp%\\logs\\log.txt" this is working fine but I want to save it to the root of current directory. 现在,我的文件路径是“ path”:“%temp%\\ logs \\ log.txt”,它工作正常,但我想将其保存到当前目录的根目录中。 For example say my app path is d:\\SampleApp\\Myapp and I want my log to save to d:\\logs\\log.txt the drive 'd' might change on deployment.
例如,假设我的应用程序路径为d:\\ SampleApp \\ Myapp,并且我希望我的日志保存到d:\\ logs \\ log.txt,则驱动器“ d”在部署时可能会更改。 So want to make it relative of which ever drive is used.
因此,要使其与所使用的驱动器相关。 I want to resolve this through appsettings.json, not through coding.
我想通过appsettings.json来解决此问题,而不是通过编码来解决。 TIA
TIA
To set your root folder where you can save a file, you need to have the direction without the root directory and then use the method Combine()
from the Path
class contained in System.IO
. 要设置您可以保存文件的根文件夹,您需要在没有根目录的情况下进行操作,然后使用
System.IO
包含的Path
类中的方法Combine()
。
To get all the drives, you would make use of
DriveInfo
class contained inSystem.IO
namespace and their attributeName
to get the root directory.要获取所有驱动器,您将利用
System.IO
命名空间中包含的DriveInfo
类及其属性Name
来获取根目录。For example:
例如:
using System.IO; ... ... DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach(DriveInfo x in allDrives) { Console.WriteLine(x.Name); } // Console: "C:\\" // Console: "D:\\"
And then you can combine the root directory path with the file path as following: 然后可以将根目录路径和文件路径组合如下:
Path.Combine(rootPath, filePathWithoutRoot);
To get the root directory of a path, you can use GetPathRoot()
method from Path
class that's contained in the namespace System.IO
. 要获取路径的根目录,可以使用命名空间
System.IO
包含的Path
类中的GetPathRoot()
方法。 This method returns to you the root directory from a path passed as string
argument. 此方法从作为
string
参数传递的路径返回到根目录。
At this page of Microsoft MSDN you can get more information and this example of how to use it:
在Microsoft MSDN的此页面上 ,您可以获得更多信息以及如何使用它的示例:
string path = @"\\mydir\\"; string fileName = "myfile.ext"; string fullPath = @"C:\\mydir\\myfile.ext"; string pathRoot; pathRoot = Path.GetPathRoot(fullPath); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathRoot); // This code produces output similar to the following: // // GetPathRoot('C:\\mydir\\myfile.ext') returns 'C:\\'
At this page from the Microsoft MSDN site you can get more information about it: 在Microsoft MSDN站点的本页上,您可以获得有关它的更多信息:
Try this (using System.IO): 试试看(使用System.IO):
string root = Path.GetPathRoot(System.Reflection.Assembly.GetEntryAssembly().Location); 字符串根= Path.GetPathRoot(System.Reflection.Assembly.GetEntryAssembly()。Location); Also, You can use Path.GetFullPath() instead Path.GetPathRoot() and remove unwanted part from the string without hardcoding the folder name And append your folder to it.
另外,您可以使用Path.GetFullPath()代替Path.GetPathRoot()并从字符串中删除不需要的部分,而无需对文件夹名称进行硬编码,然后将文件夹附加到该文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.