【问题标题】:Where is |DataDirectory| defined?|数据目录|在哪里定义?
【发布时间】:2012-08-29 21:55:16
【问题描述】:

这是Where is that file on my system?的后续问题

SO 和互联网上有大量问题和答案,但我找不到任何可以回答这个特定问题的答案。

全部默认,但我找不到文件本身,

它不存在

在哪里/如何获取|DataDirectory| 定义

文件保存在哪里,它甚至存在吗?如果不是,那是怎么回事?

编辑:文件位于AppDomain.CurrentDomain.GetData("DataDirectory").ToString();所有(分散的)答案告诉我应该是。它必须在某个地方,因为当我更改模型时,调试器会中断关于模型不等于表格的唠叨。它在那里。

【问题讨论】:

    标签: sql asp.net-mvc database entity-framework vwdexpress


    【解决方案1】:

    |DataDirectory| 本身并不是一个文件。引用一段相当老的 MSDN 文章:

    默认情况下,|DataDirectory|变量会展开如下:

    • 对于放置在用户计算机目录中的应用程序,这将是应用程序的 (.exe) 文件夹。
    • 对于在 ClickOnce 下运行的应用程序,这将是由 ClickOnce 创建的特殊数据文件夹
    • 对于 Web 应用程序,这将是 App_Data 文件夹

    在底层,|DataDirectory| 的值只是来自应用程序域上的一个属性。可以通过这样做来更改该值并覆盖默认行为:

    AppDomain.CurrentDomain.SetData("DataDirectory", newpath)
    

    关于您的架构不一致的进一步报价:

    使用本地数据库文件时要知道的一件事是它们被视为任何其他内容文件。对于桌面项目,这意味着默认情况下,每次构建项目时都会将数据库文件复制到输出文件夹(又名 bin)。 F5 之后,它在磁盘上的样子是这样的

     MyProject\Data.mdf
    
     MyProject\MyApp.vb
    
     MyProject\Bin\Debug\Data.mdf
    
     MyProject\Bin\Debug\MyApp.exe
    

    在设计时,数据工具使用 MyProject\Data.mdf。在运行时,应用程序将使用输出文件夹下的数据库。由于复制,很多人的印象是应用程序没有将数据保存到数据库文件中。其实这只是因为涉及到的数据文件有两个副本。通过数据库浏览器查看架构/数据时同样适用。这些工具使用的是项目中的副本,而不是 bin 文件夹中的那个。

    【讨论】:

    • 当你的答案进来时,答案已经输入了一半。研究得很好!
    • AppDomain.CurrentDomain.GetData("DataDirectory") 返回 NULL
    【解决方案2】:

    |数据目录|算法位于 System.Data.dll 程序集的内部 System.Data.Common.DbConnectionOptions 类中。这里它由ILSpy 显示(注意它现在在参考源存储库中可用的源:https://github.com/Microsoft/referencesource/blob/e458f8df6ded689323d4bd1a2a725ad32668aaec/System.Data.Entity/System/Data/EntityClient/DbConnectionOptions.cs):

    internal static string ExpandDataDirectory(string keyword,
                                               string value,
                                               ref string datadir)
    {
        string text = null;
        if (value != null && 
            value.StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase))
        {
            string text2 = datadir;
            if (text2 == null)
            {
                // 1st step!
                object data = AppDomain.CurrentDomain.GetData("DataDirectory");
                text2 = (data as string);
                if (data != null && text2 == null)
                    throw ADP.InvalidDataDirectory();
    
                if (ADP.IsEmpty(text2))
                {
                    // 2nd step!
                    text2 = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (text2 == null)
                {
                    text2 = "";
                }
                datadir = text2;
            }
    
            // 3rd step, checks and normalize
            int length = "|datadirectory|".Length;
            bool flag = 0 < text2.Length && text2[text2.Length - 1] == '\\';
            bool flag2 = length < value.Length && value[length] == '\\';
            if (!flag && !flag2)
            {
                text = text2 + '\\' + value.Substring(length);
            }
            else
            {
                if (flag && flag2)
                {
                    text = text2 + value.Substring(length + 1);
                }
                else
                {
                    text = text2 + value.Substring(length);
                }
            }
            if (!ADP.GetFullPath(text).StartsWith(text2, StringComparison.Ordinal))
                throw ADP.InvalidConnectionOptionValue(keyword);
        }
        return text;
    }
    

    所以它首先查看当前的 AppDomain 数据(默认情况下,我相信没有定义“DataDirectory”数据),然后到达当前的 AppDomain 基目录。剩下的主要是检查路径根和路径规范化。

    【讨论】:

      【解决方案3】:

      MSDN forum 上有一个类似但简化的问题,上面写着:

      默认情况下 |DataDirectory|指向您的应用程序文件夹(正如您在原始问题中发现的那样:指向 App_Data)。

      由于只是数据库的替代路径,您可以使用 AppDomain.SetData 自行定义路径。

      【讨论】:

      • 在 SO 和互联网上都有大量的问题和答案,但我找不到任何可以回答这个特定问题的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多