0

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
dodoria1992
  • 127
  • 1
  • 3
  • 14
  • It is not a good idea to save your files in the same folder where your app is installed. Usually the OS prevents via permissions writing in this folder. – Steve Sep 07 '15 at 17:07
  • so your advice is to leave it in AppData directory? This image is only used as a link untill user save file as .pdf or .docx. Should I programatically delete it after use? – dodoria1992 Sep 07 '15 at 17:09
  • 1) Yes and 2) probably yes – Steve Sep 07 '15 at 17:10
  • Ok. Thans for advice :) – dodoria1992 Sep 07 '15 at 17:11
  • 1
    [This answer](http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in) will give your all the possibilities, but as I have said, it is best to continue using AppData – Steve Sep 07 '15 at 17:12

1 Answers1

0

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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • yes i understood but need to check how will it work first... thank you for reply – dodoria1992 Sep 07 '15 at 17:23
  • this works, but it does not save image to any of these locations and i can't find saved image. When i run debugger it's in the bin folder, before deploy and instalation – dodoria1992 Sep 07 '15 at 17:45
  • no exception becouse it works well. I save my file as image and get it to pdf i just don't know "Image" location after create. When i run debbuger before i create installer, it goes in bin folder, but when i install my application it does not go to installation directory, documents, desktop, anywhere... – dodoria1992 Sep 07 '15 at 17:56
  • @dodoria1992 it is placed where you saved it, if you are using `Path.Combine(Application.StartupPath, "Image.jpeg")`, then see what is your `Application.StartupPath` and find it there. for example, after saving the image simply do `MessageBox.Show(Application.StartupPath)` – Reza Aghaei Sep 07 '15 at 17:58
  • yea.. show up install directory location but does not display image... it is probabily becouse of what Steve said above... anyway thanks for your time and effort :) – dodoria1992 Sep 07 '15 at 18:05
  • 1
    @dodoria1992 the reason it does not show up is likely [UAC Data Redirection](http://superuser.com/questions/425513/windows-7-locked-files-folders/425535#425535) if you are trying to write to a protected folder. – Scott Chamberlain Sep 08 '15 at 21:59
  • you are right, i found it in **%LOCALAPPDATA%** folder. Thanks alot! :) – dodoria1992 Sep 11 '15 at 15:37