Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello
I have a report say report1.rdlc in my Winforms Application which i am binding to reportViewer at runtime.so i need to get the path of the report which i am hard coding.
How can i get the path of the report so that if i install the exe of the application it can take the path automatically.
Posted
Comments
Kenneth Haugland 8-Aug-12 1:42am    
Application.StartupPAth will do.
Sergey Alexandrovich Kryukov 8-Aug-12 1:52am    
Absolutely wrong. This path can be anything as the user can start any application from any startup directory at all.
Please see my answer.
--SA
Kenneth Haugland 8-Aug-12 1:57am    
I dont really see that. That could happen with ExecutablePath as well, the documentation is not that different regarding the two:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

From those two documents its hard to tell if they didnt use the same code....
Sergey Alexandrovich Kryukov 8-Aug-12 2:00am    
I explained how you can see that in my comment to your question in comment to my answer.
And -- read these two articles again -- they clearly explain that the results of the call are different. After all, test in during run-time. Start application from Explorer or other file manager having some random current directory...
--SA
Sergey Alexandrovich Kryukov 8-Aug-12 1:51am    
Right. If you think about it, you will see that there are no cases when a hard-coded path name can be useful, unless you want to develop an application valid only for one computer. In all other cases, all path names are calculated during run time; just one case of it -- depending on executable directory.
--SA

There are several methods, but some of them give different results depending on how the application is hosted and other factors, other requires additional references. What you need is the location of the main executable module of the entry assembly.

This is the robust and universal way:
C#
string applicationLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
string applicationDirectory = System.Path.GetDirectoryName(applicationLocation);


Of course, one can use any other assembly of the application, but in some cases its directory might differ from the entry assembly. There are cases when you need that different directory. For example, the location of executing assembly.

This is the way applicable only to a Forms application:
C#
using System.Windows.Forms;

//...
string applicationDirectory = Application.ExecutablePath;


—SA
 
Share this answer
 
v2
Comments
Kenneth Haugland 8-Aug-12 1:51am    
Out of curiosity why not Application.StartupPath?
Sergey Alexandrovich Kryukov 8-Aug-12 1:57am    
I told you: this is a different path. Suppose you have application in some directory, don't move it. The executing directory is always the same. The startup directory is different each time you start the process. It is defined by the current directory of the application starting the process (such as Shell file manager, Explorer, or anything) or by the startup path prescribed in the .LNK file.
--SA
Kenneth Haugland 8-Aug-12 2:04am    
Then I learned somthing new today :) a 5...
Sergey Alexandrovich Kryukov 8-Aug-12 13:18pm    
Great. Thank you, Kenneth.
--SA
rohit24c 9-Aug-12 0:57am    
Guys i have a rdlc in my winforms Application which i am binding to
report-viewer through code so i need the path which i have hard coded.
But i need to get the path of rdlc something like this ~\rport1.rdlc
How can i achieve that ??
Please help
hi,
this code return your current path
C#
Application.ExecutablePath.ToString();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Aug-12 2:02am    
Even though it will work correctly, I voted 1 just for one detail: redundant "ToString". It shows your deep confusion about the basics. ExecutablePath already of the string type. I'm sorry, but "make no harm" principle should help you to avoid answering when you are not quite confident in the topic.
--SA
Philip Stuyck 8-Aug-12 2:27am    
just downvote and don't give such unpolite demotivating comment. It really does not help anyone.
Sergey Alexandrovich Kryukov 8-Aug-12 13:25pm    
My experience shows it helps... :-) Sorry, but answering like that confuse people and create destructive illusions in naive beginners. Did you thing about them?

You know, when someone down-votes me and gives me a good reason for that, I 1) always fix my problem and try to improve, 2) say "thank you" for the lesson. There are tens of examples like that. And you, having not a fraction of this experience, play the "offended card" and tell me I'm impolite. The vote of "1" is designed to correct people and create more or less valid reputation. What is really impolite is to tell lie.

--SA
Philip Stuyck 8-Aug-12 13:59pm    
Well if your experience shows it helps, and the person himself did not complain.
Sergey Alexandrovich Kryukov 8-Aug-12 15:10pm    
Your problem is: you do complain, instead of improving yourself. What you post here is your fault, and if you resist improvements and keep on baseless complaining, it will lead you nowhere.
--SA

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