-1

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

Jakub Čermoch
  • 431
  • 1
  • 9
  • 20
  • 1
    Your application cannot control the current directory when it is run. That is a decision made by the code that launches your application. The best you can do is change it to the directory you want as part of process startup. (Note that writing files into the application directory is a security vulnerability. Somebody can write a DLL file there, and now your program can be exploited.) – Raymond Chen Jul 06 '14 at 17:53
  • I agree with you. Using ".\" for temp or configuration is bad. Problem is, programmer of this app doesnt work here anymore and we have to release program this weekend. So we cant do complete refactoring. After release, refactoring and repairing like ".\" is planed and will be release as patch. Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory; // really works, but I still prefer set working directory in starting application by OS when starting via association. – Jakub Čermoch Jul 07 '14 at 09:08
  • As already noted, you do not control the initial current directory. If you need a specific current directory, you must set it yourself. – Raymond Chen Jul 07 '14 at 15:56

2 Answers2

2

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"));
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • Actually, this is not solution. I need set-up working directory to get equals of ".\car2.jpg" and "Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "car2.jpg")" something like this: http://superuser.com/questions/396394/how-do-i-set-an-executables-working-directory-via-the-command-line-prior-to-ex, but work it with file association, not from command line. – Jakub Čermoch Jul 06 '14 at 12:44
0

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 Don
  • 343
  • 2
  • 13