简体   繁体   English

VS 2010构建输出路径是否覆盖Assembly.GetExecutingAssembly案例?

[英]VS 2010 Build Output path overrides Assembly.GetExecutingAssembly case?

In debugging some behavior that relies on knowing the current executing assembly's path, I note that if I execute the following line in a C# VS 2010 project with no adjustments made to the build output path, the Assembly.GetExecutingAssembly is returning a case-correct path. 在调试某些依赖于了解当前正在执行的程序集路径的行为时,我注意到如果我在C#VS 2010项目中执行以下行而不对构建输出路径进行任何调整,则Assembly.GetExecutingAssembly将返回一个大小写正确的路径。

eg, 例如,

string location = Assembly.GetExecutingAssembly().Location;

shows "C:\\src\\MyProject\\MyProject\\bin\\Debug\\MyProject.exe" 显示“C:\\ src \\ MyProject \\ MyProject \\ bin \\ Debug \\ MyProject.exe”

Now, if I create a separate directory to output the assembly to, such as: C:\\src\\MyCamelCaseDir\\, and update the Build -> Output path to C:\\src\\mycamelcasedir, the code above produces the string "C:\\src\\mycamcelcasedir\\MyProject.exe". 现在,如果我创建一个单独的目录来输出程序集,例如:C:\\ src \\ MyCamelCaseDir \\,并将Build - > Output路径更新为C:\\ src \\ mycamelcasedir,上面的代码生成字符串“C: \\ SRC \\ mycamcelcasedir \\ MyProject.exe”。

The distinction being, clearly 区别在于,显然

"..\\MyCamelCaseDir".equals("..\\mycamelcasedir") 

is false, even if the OS doesn't treat paths as case-sensitive. 即使操作系统不将路径视为区分大小写,也是错误的。

I assume that running in debug mode in Visual Studio is the reason for this... but I'm still a little confused - shouldn't GetExecutingAssembly return the directory path the operating system thinks contains the assembly, case and all? 我假设在Visual Studio中以调试模式运行是原因...但我仍然有点困惑 - 不应该让GetExecutingAssembly返回操作系统认为包含程序集,大小写和所有内容的目录路径吗?

EDIT : I don't think my question was well phrased. 编辑 :我认为我的问题没有得到很好的表达。 The correct answer to my question is the poster who notes that VS is just concatenating the text box in Build Ouput Path plus project name. 我的问题的正确答案是海报谁注意到VS只是连接Build Ouput Path中的文本框加上项目名称。

The question I was trying to ask was: why doesn't Assembly.GetExecutingAssembly().Location, return the path with case sensitivity the way the operating system is storing it? 我试图问的问题是:为什么没有Assembly.GetExecutingAssembly()。位置,以区分大小写的方式返回操作系统存储它的路径?

I know that Windows is case insensitive in that you can type C:\\foo\\bar into an explorer window, and that will take you to C:\\Foo\\Bar (if there is such a directory). 我知道Windows不区分大小写,因为您可以在浏览器窗口中键入C:\\ foo \\ bar,这会将您带到C:\\ Foo \\ Bar(如果有这样的目录)。

But I would have thought that the location of the executing assembly would be the same in all cases, debug or not. 但我认为执行程序集的位置在所有情况下都是相同的,无论是否调试。

While the documentation is unrevealing on this point, the path from GetExecutingAssembly().Location appears to be the path the operating system used when loading the assembly (For executables, this seems to be the path handed to Process.Start() or its equivalent, which the OS uses as is, without normalizing its case, etc., to the version recorded in the filesystem.), except for the filename part, which is taken from the actual name of the assembly. 虽然文档在这一点上没有说明,但是来自GetExecutingAssembly()。Location的路径似乎是操作系统在加载程序集时使用的路径(对于可执行文件,这似乎是传递给Process.Start()或其等价物的路径操作系统按原样使用,而不将其大小写等于文件系统中记录的版本。),文件名部分除外,它取自程序集的实际名称。

You can see this in action easily by compiling a test command-line app and running it from the command prompt: 通过编译测试命令行应用程序并从命令提示符运行它,您可以轻松地看到这一点:

using System;
using System.Reflection;

namespace TestLocation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0}", Assembly.GetExecutingAssembly().Location);
        }
    }
}

Which produces: 哪个产生:

C:\\Users\\avatar>C:\\Working\\TestLocation\\bin\\Debug\\TestLocation.exe C:\\Working\\TestLocation\\bin\\Debug\\TestLocation.exe C:\\ Users \\ avatar> C:\\ Working \\ TestLocation \\ bin \\ Debug \\ TestLocation.exe C:\\ Working \\ TestLocation \\ bin \\ Debug \\ TestLocation.exe

C:\\Users\\avatar>c:\\working\\testlocation\\bin\\debug\\testlocation.exe c:\\working\\testlocation\\bin\\debug\\TestLocation.exe C:\\ Users \\ avatar> c:\\ working \\ testlocation \\ bin \\ debug \\ testlocation.exe c:\\ working \\ testlocation \\ bin \\ debug \\ TestLocation.exe

If you enter a short name, ie, just "testlocation", you get the correctly-cased version, but in the second case above, it already has a canonical filename, so just passes it through as-is - hence the name is not cased. 如果您输入一个简短的名称,即只是“testlocation”,您将获得正确的版本,但在上面的第二种情况下,它已经有一个规范的文件名,所以只是按原样传递它 - 因此名称不是套管。

In short: it is very much advisable to not rely on case-preservation in filespecs under Windows, even if the NTFS filesystem does provide it internally, unless you manually look up the case of everything from the filesystem yourself. 简而言之:非常明智的做法是不依赖于Windows下的filespecs中的case-preservation,即使NTFS文件系统确实在内部提供它,除非你自己手动查看文件系统中的所有内容。 All you're going to get from most APIs is a filespec that can be used to reach the file, not necessarily its as-recorded-in-the-filesystem filespec. 您从大多数API获得的所有内容都是可用于访问文件的filespec,而不一定是文件系统文件中记录的文件。

String comparison have no knowledge of what underlying value of the string is. 字符串比较不知道字符串的底层值是什么。 As you've pointed out by default path in Windows are not case sensitive, so you must treat them this way when comparing. 正如您在Windows中默认路径指出的那样,区分大小写不敏感,因此在比较时必须以这种方式对待它们。

Ie in your case you should be using 即在你的情况下,你应该使用

String.Equals("..\\MyCamelCaseDir", "..\\mycamelcasedir", 
    String.Comparison.OrdinalIgnoreCase) 

to compare file path strings. 比较文件路径字符串。 (Or you can normalize paths first - Normalize directory names in C# ). (或者您可以首先规范化路径 - 在C#中规范化目录名称 )。

Side note: most likely reason of different casing comes from the fact that your full executable name is constructed by VS with string concatenation of "output path" and "project name". 旁注:不同外壳的最可能原因来自于完整的可执行文件名由VS构造,其中包含“输出路径”和“项目名称”的字符串连接。

暂无
暂无

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

相关问题 Windows phone 8.1 Assembly.GetExecutingAssembly不可用 - Windows phone 8.1 Assembly.GetExecutingAssembly not available 为什么 Assembly.GetExecutingAssembly() 会返回 null? - Why would Assembly.GetExecutingAssembly() return null? 使用Assembly.GetExecutingAssembly()从其他类库获取程序集 - Use Assembly.GetExecutingAssembly() to get assembly from other class library Assembly.GetExecutingAssembly()和typeof(程序)之间的区别.Assembly - Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly Assembly.GetExecutingAssembly()。CreateInstance应该在这里抛出异常吗? - should Assembly.GetExecutingAssembly().CreateInstance throw an exception here? Fody 和 Assembly.GetExecutingAssembly().Location 返回空字符串 - Fody and Assembly.GetExecutingAssembly().Location returns empty string 与 C# 中的 Assembly.GetExecutingAssembly().GetTypes() 相关的问题 - Issue Related to Assembly.GetExecutingAssembly().GetTypes() in C# NUnit运行多个程序集时Assembly.GetExecutingAssembly的意外位置 - Unexpected Location of Assembly.GetExecutingAssembly When NUnit Runs Multiple Assemblies 这个.GetType()。Assembly.GetName()。版本和Assembly.GetExecutingAssembly()。GetName()。版本有什么区别? - What's the difference between this.GetType().Assembly.GetName().Version and Assembly.GetExecutingAssembly().GetName().Version? Assembly.GetExecutingAssembly()。GetManifestResourceNames()是否从App_LocalResources返回资源? - Does Assembly.GetExecutingAssembly().GetManifestResourceNames() return resources from App_LocalResources?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM