Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm
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 7-Aug-12 19:40pm
Comments
Kenneth Haugland at 8-Aug-12 1:42am
   
Application.StartupPAth will do.
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 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
Volynsky Alex at 8-Aug-12 18:53pm
   
+5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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:
using System.Windows.Forms;
 
//...
string applicationDirectory = Application.ExecutablePath;
 
—SA
  Permalink  
v2
Comments
Kenneth Haugland at 8-Aug-12 1:51am
   
Out of curiosity why not Application.StartupPath?
Sergey Alexandrovich Kryukov at 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 at 8-Aug-12 2:04am
   
Then I learned somthing new today :) a 5...
Sergey Alexandrovich Kryukov at 8-Aug-12 13:18pm
   
Great. Thank you, Kenneth.
--SA
rohit24c at 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
Sergey Alexandrovich Kryukov at 9-Aug-12 13:20pm
   
What's wrong with System.Web.HttpServerUtility.MapPath method?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi,
this code return your current path
Application.ExecutablePath.ToString();
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 8-Aug-12 2:27am
   
just downvote and don't give such unpolite demotivating comment. It really does not help anyone.
Sergey Alexandrovich Kryukov at 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 at 8-Aug-12 13:59pm
   
Well if your experience shows it helps, and the person himself did not complain.
Sergey Alexandrovich Kryukov at 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
Philip Stuyck at 8-Aug-12 15:54pm
   
Come on man. Now you are jumping to conclusions. Anyway this is not worth to fight about, just have a beer on me and forget I mentioned anything.
Sergey Alexandrovich Kryukov at 8-Aug-12 16:29pm
   
No reason to fight about anything, of course. I only wish you the best of luck,
--SA

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

  Print Answers RSS
0 CHill60 575
1 Sergey Alexandrovich Kryukov 499
2 OriginalGriff 325
3 PIEBALDconsult 285
4 BillWoodruff 193


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 8 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100