繁体   English   中英

如何获取 UWP 项目的根文件夹路径?

[英]How to get root folder path for UWP project?

要使用 NLog 库创建日志文件,我们必须获取特定平台的根文件夹路径。

在 iOS 我们可以这样得到:

[assembly: Dependency(typeof(GetRootFolder))]
namespace Sample.iOS.DependencyServices
{
    public class GetRootFolder : IGetRootFolder
    {
        string IGetRootFolder.GetRootFolder()
        {
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            return folder;
        }
    }
}

对于 Android 我们有这样的方式:

[assembly: Dependency(typeof(GetRootFolder))]

namespace Sample.Droid.DependencyServices
{
    public class GetRootFolder : IGetRootFolder
    {

        string IGetRootFolder.GetRootFolder()
        {
            var folder = $"{Android.OS.Environment.GetExternalStoragePublicDirectory("Log").CanonicalPath}";
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            return folder;
        }
    }
}

我们如何在 Xamarin.Forms 中获取 UWP 项目的文件夹路径?

在 UWP 中,您可以使用以下代码获取应用程序的安装文件夹。

[assembly: Dependency(typeof(GetPath))]
namespace App1.UWP
{
class GetPath : IGetPath
{
    public void Getpath()
    {
        // Get the app's installation folder.
        StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        // Print the folder's path to the Visual Studio Output window.
        Debug.WriteLine(appFolder.Name + " folder path: " + appFolder.Path);
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM