簡體   English   中英

如何以編程方式設置此 xml 文件的物理路徑?

[英]How can I set the physical path of this xml file programmatically?

如圖所示,我將 hibernate.cfg.xml congif 文件放在 PersistenceManager 項目中。

在此處輸入圖像描述

我需要以編程方式在此 getter 中設置此配置文件的物理路徑以配置 NHibernate (帶有 cfg.Configure 的行):

public class SessionService
{
    private static ISessionFactory _sessionFactory = null;
    public static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                Configuration cfg = new Configuration();
                string fullPath = (new SessionService()).GetType().Assembly.Location;

                cfg.Configure(@"the working path to hibernate.cfg.xml");

                //I will Add Mapping directives here

                _sessionFactory = cfg.BuildSessionFactory();
            }

            return _sessionFactory;
        }
    }
}

我怎樣才能通過鍵入字符串“hibernate.cfg.xml”並讓 C# 生成物理路徑的 rest 來安全地做到這一點?

在文件的屬性 window 中,將“復制到 Output 目錄”設置為“如果較新則復制”。 然后應該通過Configure方法找到它,而無需添加路徑(只是文件名)。

編輯:要在運行時獲取完整路徑,您可以嘗試以下操作:

cfg.Configure(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"hibernate.cfg.xml"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM