简体   繁体   中英

Folder of running application

I have problem with my C# app, when is opened via file association, it works in file directory. 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").

So, I think, when app is opened via file association, it run with working folder of that file ("C:\\Photo\\"). Is there way, how to keep working directory as directory with app.exe?

Edit:

This is not solution, I need to get equals of ".\\" and 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:

 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:

public static string fileOpen;
public static string workingDirectory;

In your app.xaml.cs file, add the following code:

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. This code saves the directory.

Back to your MainWindow.xaml.cs file:

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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM