简体   繁体   中英

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

In my win forms C# app, I am exporting my images to pdf and word. Before export, images need to be saved as bitmap. Did it like this:

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

Now the code for word and pdf export read this file normally from saved location. Howewer, while I was testing my desktop app, code "Image.jpeg" saves image to bin directory.

When I made installer using InstallShield and installed my program, this option works but it save my image to desktop. I don't really want that.

Managed to send it to ApplicationData directory but don't want that either...

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

How to navigate my file to installation directory?

Simply use 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.

For example you can use it this way:

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

For ClickOnce applications, you can use ApplicationDeployment.CurrentDeployment.DataDirectory , for more information, see Accessing Local and Remote Data in ClickOnce Applications

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