代码之家  ›  专栏  ›  技术社区  ›  Joey Gumbo

从WPF应用程序获取应用程序的目录

  •  153
  • Joey Gumbo  · 技术社区  · 16 年前

    我找到了带AppDomain的Windows窗体的解决方案,但是对于WPF来说,什么是等效的解决方案? Application 对象?

    8 回复  |  直到 16 年前
        1
  •  306
  •   Doug    10 年前

    一种方法:

    System.AppDomain.CurrentDomain.BaseDirectory
    

    另一种方法是:

    System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
    
        2
  •  27
  •   Juan Manuel Serrat BS_C3    13 年前

    下面是另一个:

    System.Reflection.Assembly.GetExecutingAssembly().Location
    
        3
  •  7
  •   Dr. Rajesh Rolen    13 年前

    还可以使用命令行参数的第一个参数:

    String exePath = System.Environment.GetCommandLineArgs()[0]

        4
  •  3
  •   Arsen Mkrtchyan    16 年前
    String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
     string dir = Path.GetDirectoryName(exePath);
    

    试试这个!

        5
  •  3
  •   Roshan J    13 年前

    试试这个。别忘了 using System.Reflection .

    string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    
        6
  •  3
  •   QMaster    12 年前

    我简单地用过 string baseDir = Environment.CurrentDirectory; 对我来说也是如此。

    祝你好运

    编辑:

    我曾经删除过这类错误,但我更喜欢编辑它,因为我认为这个答案的负点可以帮助人们了解错误的方法。:)我理解上述解决方案不有用,我将其更改为 string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory; 其他方法是:

    1. string baseDir =   
        System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
     2. String exePath = System.Environment.GetCommandLineArgs()[0];
     3. string appBaseDir =    System.IO.Path.GetDirectoryName
        (System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
    

    祝你好运

        7
  •  0
  •   crash    14 年前

    也可以从system.windows.forms中自由使用application.startuppath,但必须添加对system.windows.forms程序集的引用!

        8
  •  0
  •   paul    11 年前

    我试过这个:

        label1.Content = Directory.GetCurrentDirectory();
    

    并获取目录。