简体   繁体   English

正在运行的应用程序的文件夹

[英]Folder of running application

I have problem with my C# app, when is opened via file association, it works in file directory. 我的C#应用​​程序有问题,通过文件关联打开时,它在文件目录中有效。 For example, when I create copy of opened file: 例如,当我创建打开文件的副本时:

File.Copy("C:\Photo\car.jpg", ".\car2.jpg"); // this is only ilustration code.

It makes new file "C:\\Photo\\car2.jpg", but I want to make file in my app directory (".\\car2.jpg"). 它创建了新文件“ C:\\ Photo \\ car2.jpg”,但我想在我的应用程序目录中创建文件(“。\\ car2.jpg”)。

So, I think, when app is opened via file association, it run with working folder of that file ("C:\\Photo\\"). 因此,我认为,当通过文件关联打开应用程序时,它将在该文件的工作文件夹(“ C:\\ Photo \\”)下运行。 Is there way, how to keep working directory as directory with app.exe? 有没有办法,如何使用app.exe将工作目录保留为目录?

Edit: 编辑:

This is not solution, I need to get equals of ".\\" and System.AppDomain.CurrentDomain.BaseDirectory: 这不是解决方案,我需要等于“。\\”和System.AppDomain.CurrentDomain.BaseDirectory:

File.Copy("C:\Photo\car.jpg", Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "car2.jpg"));

I have to use this on many places in application, solution can be sets: 我必须在应用程序中的许多地方使用它,可以设置解决方案:

Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

but I prefer set it in startup application via file association and not in running program - it looks cleaner. 但是我更喜欢通过文件关联而不是在运行的程序中在启动应用程序中设置它-看起来更干净。

Thanks, Jakub 谢谢,雅库布

To get the path of your application, you can use: 要获取应用程序的路径,可以使用:

 System.AppDomain.CurrentDomain.BaseDirectory

Use Path.Combine to build the destination path as follows: 使用Path.Combine如下构建目标路径:

 File.Copy("C:\Photo\car.jpg", Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "car2.jpg"));

I'd like to try and offer an alternative. 我想尝试提供替代方法。 I'm relatively new to this, but I figured out a solution that works for me: 我相对较新,但是我想出了一个对我有用的解决方案:

Make 2 static variables in your MainWindow.xaml.cs: 在MainWindow.xaml.cs中创建2个静态变量:

public static string fileOpen;
public static string workingDirectory;

In your app.xaml.cs file, add the following code: 在您的app.xaml.cs文件中,添加以下代码:

protected override void OnStartup(StartupEventArgs e)
{
    if (e.Args.Count() > 0)
    {
        var a = File.Exists(e.Args[0]);
        var path = Path.GetFullPath(e.Args[0]);
        MainICPUI.workingDirectory = Directory.GetCurrentDirectory();
        MainICPUI.fileOpen = e.Args[0];
    }

    base.OnStartup(e);
}

When you open a file associated with your program from any directory, the full file name is added to the StartupEventArgs, including the directory. 当您从任何目录打开与程序关联的文件时,完整的文件名将添加到StartupEventArgs(包括目录)中。 This code saves the directory. 此代码保存目录。

Back to your MainWindow.xaml.cs file: 返回您的MainWindow.xaml.cs文件:

public static string fileOpen;
public static string workingDirectory;

public MainWindow()
{
    InitializeComponent();

    Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
    // This sets the directory back to the program directory, so you can do what you need
    // to with files associated with your program
}

// Make sure your MainWindow has an OnLoaded event assigned to:
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
    // Now I'm setting the working directory back to the file location
    Directory.SetCurrentDirectory(workingDirectory);

    if (File.Exists(fileOpen))
    {
        var path = Path.GetFullPath(fileOpen);
        // This should be the full file path of the file you clicked on.
    }
}

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

相关问题 防止应用程序在错误的文件夹中运行 - Prevent Application from running inside the wrong folder 使用 C# 获取正在运行的应用程序的安装文件夹 - Getting the Installation Folder of Running Application using C# 从system32文件夹运行控制台应用程序 - Running a console application from the system32 folder 访问在 azure 服务器上运行的 .net 核心应用程序中的共享视图文件夹 - Access shared view folder in a .net core application running on an azure server 在应用程序文件夹中运行时,无法使自定义错误页面在ASP.NET MVC应用程序中工作 - Cannot make custom error pages work in ASP.NET MVC application when running in application folder 文件夹/目录锁定,因此只有正在运行的应用程序(和子进程)才能对其进行修改 - Folder / Directory lock so only running application (and child processes) can modify it 为我的应用程序运行 Wix 安装程序后,无法创建/写入文件/文件夹,即使删除文件夹后也是如此 - Cannot create/write to files/folders after running a Wix Installer for my application, even after deleting folder 在ClickOnce应用程序中包含一个文件夹 - Include a Folder in ClickOnce Application 在Web应用程序中选择一个文件夹 - Select a folder in web application WinForms应用程序中的Images文件夹 - Images folder in WinForms application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM