C# 如何使用C获取当前项目目录路径#

C# 如何使用C获取当前项目目录路径#,c#,.net,C#,.net,好的,问题来了。我有两个项目,一个是C#控制台,另一个是类库。 我正在从控制台应用程序访问/调用类库方法。 类库项目中有一个名为“文件”的文件夹 我需要获得类库的files文件夹的路径,但是每当我使用 System.IO.Directory.GetCurrentDirectory(); 及 它给了我控制台项目的路径,我用它来调用方法 上面的方法给了我一条路径 C:\\ConsolePro\\bin\\Debug 但我需要类库项目的路径 C:\\ClassLibPro\\bin\\Debug

好的,问题来了。我有两个项目,一个是C#控制台,另一个是类库。 我正在从控制台应用程序访问/调用类库方法。 类库项目中有一个名为“文件”的文件夹

我需要获得类库的files文件夹的路径,但是每当我使用

System.IO.Directory.GetCurrentDirectory();

它给了我控制台项目的路径,我用它来调用方法

上面的方法给了我一条路径

C:\\ConsolePro\\bin\\Debug
但我需要类库项目的路径

C:\\ClassLibPro\\bin\\Debug

请建议

您应该能够多次使用
Directory.GetParent(Directory.GetCurrentDirectory())
来获取更高级别的目录,然后将lib目录的路径添加到该目录的末尾。

如果您从其他程序集加载类库

string Path = System.Reflection.Assembly.GetAssembly(typeof({LibraryClassName})).Location;

string PathToClassLibPro = Path.GetDirectoryName( Path);

用库的类名替换
{LibraryClassName}

一旦编译并运行了代码,“项目路径”就没有意义了。您只能确定已编译程序集的文件位置。如果您的控制台项目直接引用构建的“类库”DLL,而不是通过项目引用,则只能执行您所要求的操作

然后,您可以利用反射来获得装配路径,如

string path = Assembly.GetAssembly(typeof (SomeClassInOtherProject)).Location;

我相信问题是:

由于控制台项目具有DLL文件引用,因此它正在使用DLL调用任何方法。 此时,它返回类库项目的DLL位置,该位置位于控制台项目的bin目录中,它不知道类库项目的物理位置


所以本质上它返回的是相同的项目路径。为了解决这个问题,我必须将两个项目移到同一个目录中。

我推荐两个选项之一

  • 如果文件很小,则将其包含在类库中,并在需要时将其流式传输到临时位置

  • 另一个选项是在构建期间将文件复制到输出目录,并以这种方式使用它们。对于多个共享项目,最好有一个公共bin文件夹,将程序集复制到该位置并从中运行


  • 我希望我正确地理解你:

    Path.GetDirectoryName(typeof(Foo.MyFooClass).Assembly.Location);
    

    尽管我找不到好的解决方案,但我还是使用了以下技巧: 只要你想回到理想的路径,你就应该添加Directory.GetParent()而不是

      Directory.GetParent(...(Directory.GetParent(Directory.GetCurrentDirectory()).ToString()...).ToString()
    

    我使用以下方法在运行时获取当前项目路径:

    public static class ProjectInfo {
       public static string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
       public static string projectPath = appDirectory.Substring(0, appDirectory.IndexOf("\\bin"));
    }
    

    我也遇到了同样的问题,无法访问命名空间的bin/debug文件夹中的文件。我的解决方案是使用
    Split()
    操作字符串,然后构造一个新字符串,它是我命名空间中json文件的绝对路径

    private static string GetFilePath()
            {            
                const char Escape = '\\'; //can't have '\' by itself, it'll throw the "Newline in constant" error
                string directory = Environment.CurrentDirectory;
                string[] pathOccurences = directory.Split(Escape);            
                string pathToReturn = pathOccurences[0] + Escape; //prevents index out of bounds in upcoming loop
                for(int i = 1; i < pathOccurences.Length; i++)
                {
                    if (pathOccurences[i] != pathOccurences[i - 1]) //the project file name and the namespace file name are the same
                        pathToReturn += pathOccurences[i] + Escape;
                    else
                        pathToReturn += typeof(thisClass).Namespace + Escape; //In the one occurrence of the duplicate substring, I replace it with my class Namespace name
                }
                return pathToReturn + "yourFile.json";
            }
    
    私有静态字符串GetFilePath()
    {            
    const char Escape='\\';//不能单独使用'\',它将抛出“Newline in constant”错误
    string directory=Environment.CurrentDirectory;
    字符串[]pathccurrences=directory.Split(转义);
    string pathToReturn=pathccurrences[0]+Escape;//防止索引在即将到来的循环中越界
    for(int i=1;i

    我个人不喜欢这个解决方案,但这是我能想到的唯一答案。

    不,这不是答案。我需要知道如何获取文件所在的当前项目路径。请把问题再读一遍!这不管用。路径是位于控制台项目中的DLL。如何在{assembly}位置定义程序集类型。我使用如下字符串Path=System.Reflection.Assembly.getExecutionGassembly().Location;您使用了哪个类名?你的图书馆专题课?为什么它不起作用,错误的路径?检查我的答案。我已经解释过了。您的代码不会返回位置,但会返回控制台项目中的DLL位置。所以本质上它返回的是相同的项目路径。为了解决此问题,我必须将两个项目移动到同一目录中。仅供参考,您将永远无法获得dll目录的路径,因为Visual Studio会将依赖项从其原始位置(ClassLibPro)复制到项目的bin/debug文件夹(Console Pro)。在运行时无法追溯到这一点。为什么需要项目的路径?
    private static string GetFilePath()
            {            
                const char Escape = '\\'; //can't have '\' by itself, it'll throw the "Newline in constant" error
                string directory = Environment.CurrentDirectory;
                string[] pathOccurences = directory.Split(Escape);            
                string pathToReturn = pathOccurences[0] + Escape; //prevents index out of bounds in upcoming loop
                for(int i = 1; i < pathOccurences.Length; i++)
                {
                    if (pathOccurences[i] != pathOccurences[i - 1]) //the project file name and the namespace file name are the same
                        pathToReturn += pathOccurences[i] + Escape;
                    else
                        pathToReturn += typeof(thisClass).Namespace + Escape; //In the one occurrence of the duplicate substring, I replace it with my class Namespace name
                }
                return pathToReturn + "yourFile.json";
            }