而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。

我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。


当前回答

Try:

            {
                OpenFileDialog fd = new OpenFileDialog();
                fd.Multiselect = false;
                fd.Filter = "Image files (*.bmp, *.jpg)|*.bmp;*.jpg|All files (*.*)|*.*";
                if (fd.ShowDialog() == true)
                {
                    if (fd.CheckFileExists)
                    {
                        var fileNameToSave = GetTimestamp(DateTime.Now) + Path.GetExtension(fd.FileName);
                        var pathRegex = new Regex(@"\\bin(\\x86|\\x64)?\\(Debug|Release)$", RegexOptions.Compiled);
                        var directory = pathRegex.Replace(Directory.GetCurrentDirectory(), String.Empty);
                        var imagePath = Path.Combine(directory + @"\Uploads\" + fileNameToSave);
                        File.Copy(fd.FileName, imagePath);

                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

这是上传图片到WPF上传目录的代码

其他回答

最好的解决方案

string PjFolder1 =
    Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).
        Parent.Parent.FullName;

其他解决方案

string pjFolder2 = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)));

测试它,AppDomain.CurrentDomain.BaseDirectory在过去的项目中为我工作,现在我得到调试文件夹....所选的好答案就是不行!

//Project DEBUG folder, but STILL PROJECT FOLDER
string pjDebugFolder = AppDomain.CurrentDomain.BaseDirectory;

//Visual studio folder, NOT PROJECT FOLDER
//This solutions just not work
string vsFolder = Directory.GetCurrentDirectory();
string vsFolder2 = Environment.CurrentDirectory;
string vsFolder3 = Path.GetFullPath(".\\");   

//Current PROJECT FOLDER
string ProjectFolder = 
    //Get Debug Folder object from BaseDirectory ( the same with end slash)
    Directory.GetParent(pjDebugFolder).
    Parent.//Bin Folder object
    Parent. //Project Folder object
    FullName;//Project Folder complete path

获取c#项目根文件夹的正确方法是利用[CallerFilePath]属性获取源文件的完整路径名,然后减去文件名加扩展名,留下项目的路径。

下面是如何做到这一点:

在项目的根文件夹中,添加包含以下内容的ProjectSourcePath.cs文件:

internal static class ProjectSourcePath
{
    private const  string  myRelativePath = nameof(ProjectSourcePath) + ".cs";
    private static string? lazyValue;
    public  static string  Value => lazyValue ??= calculatePath();

    private static string calculatePath()
    {
        string pathName = GetSourceFilePathName();
        Assert( pathName.EndsWith( myRelativePath, StringComparison.Ordinal ) );
        return pathName.Substring( 0, pathName.Length - myRelativePath.Length );
    }
}

字符串?需要一个非常晚的c#版本#nullable enable;如果你没有,那就去掉?。

Assert()函数是我自己的;如果你喜欢过危险的生活,你可以用你自己的代替它,或者省略它。

函数GetSourceFilePathName()的定义如下:

using System.Runtime.CompilerServices

    public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) //
        => callerFilePath ?? "";

一旦你有了以上这些,你可以这样使用它:

string projectSourcePath = ProjectSourcePath.Value;

1 'proper'在:万无一失的;可靠;没有假设;不牢靠的:不牢靠的;不一定在某些项目上成功,但在另一些项目上失败;当你改变不相关的东西时,不太可能毫无征兆地坏掉;等。

如果项目在IIS express上运行,则环境。CurrentDirectory可以指向IIS Express所在的位置(默认路径是C:\Program Files (x86)\IIS Express),而不是指向项目所在的位置。


这可能是各种项目最适合的目录路径。

AppDomain.CurrentDomain.BaseDirectory

这就是MSDN的定义。

获取程序集解析器用于探测程序集的基目录。

还有另一个不完美的解决方案(但可能比其他一些更接近完美):

    protected static string GetSolutionFSPath() {
        return System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName;
    }
    protected static string GetProjectFSPath() {
        return String.Format("{0}\\{1}", GetSolutionFSPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
    }

这个版本将返回当前项目的文件夹,即使当前项目不是解决方案的启动项目。

第一个缺陷是我跳过了所有的错误检查。这很容易解决,但只有当你将项目存储在驱动器的根目录中或在路径中使用连接(并且该连接是解决方案文件夹的后代)时才会成为问题,所以这种情况不太可能发生。我不完全确定Visual Studio是否能够处理这两种设置。

您可能遇到的另一个(更可能的)问题是,项目名称必须与项目的文件夹名称匹配才能找到它。

您可能遇到的另一个问题是项目必须在解决方案文件夹中。这通常不是问题,但如果您使用“将现有项目添加到解决方案”选项将项目添加到解决方案中,那么这可能不是解决方案的组织方式。

最后,如果您的应用程序将修改工作目录,您应该在修改之前存储这个值,因为这个值是相对于当前工作目录确定的。

当然,这也意味着您不能在项目属性对话框中更改项目的“构建->输出路径”或“调试->工作目录”选项的默认值。

string projPath = Path.GetFullPath(@"..\..\..\");
Console.WriteLine(projPath);

这对我来说一直都很有效。试一试。