Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi i'm developing windows application in c#.While developing the pdf file i want to save into current working directory.That means in the client machine the pdf file should be save into application directory.It shouldn't ask anything,where it would save.But it should save in the current directory that means where client is installing the application in the system.
Posted
Updated 14-May-15 5:01am
v2
Comments
Richard MacCutchan 14-May-15 11:02am    
That is not always possible, since application directories tend to be protected.
Sergey Alexandrovich Kryukov 14-May-15 11:58am    
The inquires asks not about application directory, but about working directory. They are, generally, different directories.
—SA
Richard MacCutchan 14-May-15 12:03pm    
Read the last sentence again; it is quite ambiguous.
Sergey Alexandrovich Kryukov 14-May-15 12:12pm    
Yes, of course, but this is because the inquirer is quite confused. But thank you for the note, it makes your warning perfectly valid and important. I added a new paragraph to my answer, to try to un-confuse him.
—SA
Divakard3 14-May-15 11:07am    
So where should it save the file?

If this is an application with UI, the best option for most cases would be using "Save As" dialog, so the user would be able to choose the directory.

However, using a working directory is quite typical, especially for console-only applications. Apparently, this directory could still be considered as a part of the user input: this is the directory where the user started the application. If you do it this way, you can use the file name without path, because the working directory will have precedence. Note that the application directory and working directory are different directories: a user can start the application in any directory.

[EDIT]

Also pay attention for our discussion with Richard MacCutchan, who gave you important warning about application directory. This is the directory where you executable module is placed (which is, in turn, has nothing to do with the installation; the application can work without any installations). Generally, this directory is intended to provide only read-only access to files.

Working directory is something different: this is where the user chooses to start the application (and the application can later change it), the default directory for relative path names. If the user choose the directory with write access (which is can be well expected), writing will be possible. :-)

[END EDIT]

For finding the directories associated with some application explicitly, please see my past answers:
How to find my programs directory (executable directory),
How to find my programs directory (current directory, "special folders").

Never use string concatenation and other string operation for finding file names and path names. Note some useful methods of the class System.IO.Path, in particular:
https://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx[^] (do not concatenate, use these methods!),
https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.110).aspx[^], and so on.

—SA
 
Share this answer
 
v3
Ok, for doing this job you need to figure out about current execution file path, in .Net for this purpose we have several ways some of the most useful ways are:
First part:
C#
string exePath = Application.ExecutablePath;
string startupPath = Application.StartupPath;

Or
C#
System.Reflection.Assembly.GetEntryAssembly().Location;

Or
C#
string path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );


But I prefer first one. ;-)
Then you need to make sure you have a read and write permission at this directory, basically when you are installing application under one of the system users you will receive demanded privilege from user account service so you don`t need to care about it, however you can also set other user and permissions at the second part of this job.

Second Part:
This part is really depend on your PDF creation module, but by assuming the most poplar way to doing this business you will need to use System.IO library. But for setting permissions try to look at the below links:
FileStream Class:
https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx[^]
Directory.SetAccessControl Method:
https://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol(v=vs.110).aspx[^]
FileInfo.SetAccessControl Method:
https://msdn.microsoft.com/en-us/library/system.io.fileinfo.setaccesscontrol(v=vs.110).aspx[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-May-15 12:09pm    
This is all good, but you completely ignored what the inquirer wanted: working directory.
And using working directory, in some cases, is more reasonable.
—SA
Aydin Homay 15-May-15 5:48am    
Sergey it dose not make difference, what you need you need to know about the above library so and what you suggested in your solution is not something else than System.IO.Path
Got an answer to save in current application startup path.

string filesave = String.Format(@"{0}", Application.StartupPath);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900