安装已发布的 C# win 窗体桌面应用程序后,将文件保存到安装目录而不是桌面

Save files to install directory instead of desktop after installing published C# win forms desktop app

在我的 win forms C# 应用程序中,我将图像导出为 pdf 和 word。导出前,图像需要保存为位图。是不是这样:

// code
bitmap.Save("Image.jpeg", ImageFormat.Jpeg);
bitmap.Dispose();

现在,word 和 pdf 导出代码可以正常从保存的位置读取该文件。然而,当我测试我的桌面应用程序时,代码 "Image.jpeg" 将图像保存到 bin 目录。

当我使用 InstallShield 创建安装程序并安装我的程序时,此选项有效但它会将我的图像保存到桌面。我真的不想那样。

设法将它发送到 ApplicationData 目录,但也不想要...

string imageSaved = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Image.jpeg");
bitmap.Save(imageSaved, ImageFormat.Jpeg);
bitmap.Dispose();

如何将我的文件导航到安装目录?

只需使用Application.StartupPath

Gets the path for the executable file that started the application, not including the executable name.

The path for the executable file that started the application. This path will be different depending on whether the Windows Forms application is deployed using ClickOnce. ClickOnce applications are stored in a per-user application cache in the C:\Documents and Settings\username directory.

例如你可以这样使用它:

string imageSaved = Path.Combine(Application.StartupPath, "Image.jpeg");
bitmap.Save(imageSaved, ImageFormat.Jpeg);

对于 ClickOnce 应用程序,您可以使用 ApplicationDeployment.CurrentDeployment.DataDirectory, for more information, see Accessing Local and Remote Data in ClickOnce Applications