[英]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.