Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,060 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,585 people online right now. Registration is fast and FREE... Join Now!




i need to find the directory my program is installed in.

 
Reply to this topicStart new topic

i need to find the directory my program is installed in.

Extas
18 Apr, 2008 - 06:27 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 9

basically the topic says it all.

i have an app that when the user clicks print it makes a txt file and executes it with notepad so they can adjust font or size and print from there,(i dont work with printing yet) everything works fine except one problem.

i save the txt document using
CODE

System.IO.StreamWriter tw = new System.IO.StreamWriter("C:\\tempprint.txt");
tw.WriteLine(listViewContent.ToString());
tw.Close();


this works perfect on my computer, but i've tested it on other computers and some of them tell me that access is denied (cause it wont let you write directly to the c drive) and the program crashes. i put a try catch around it to explain to the user what the problem is instead of crashing but what id like to be able to do is get a string variable for the directory my exe is stored in and use that instead of ("C:\\tempprint.txt");

not to mention some crazy ass may not have a "C" drive.

thanks in advance for any help.

This post has been edited by Extas: 18 Apr, 2008 - 06:29 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: I Need To Find The Directory My Program Is Installed In.
18 Apr, 2008 - 06:39 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,018



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Anything wrong with System.IO.StreamWriter tw = new System.IO.StreamWriter(@"tempprint.txt");?

If you want to get technical about it, it's the same as:
CODE

System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + "tempprint.txt"


However, for maximum tempfile goodness, you might consider the method "System.IO.Path.GetTempFileName()"

Hope this helps.

User is online!Profile CardPM
+Quote Post

Extas
RE: I Need To Find The Directory My Program Is Installed In.
18 Apr, 2008 - 07:04 AM
Post #3

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 9

thanks for you info, after seeing what you said about System.IO.GetTempFileName();

i was able to make it work using this

CODE

string executePath = System.IO.GetTempPath() + "tempprint.txt";



then i just used that variable for saving the file as well as plugged it in for the path for notpad to execute.

thanks again!

This post has been edited by Extas: 18 Apr, 2008 - 07:30 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: I Need To Find The Directory My Program Is Installed In.
18 Apr, 2008 - 07:40 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,018



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Happy to help!

Still, why not use the "GetTempFileName()"? I know, you really like "tempprint.txt", but consider

Having a staticly named file means if the same program runs more than once, the instances clobber each other. If your program crashed, how does it deal with the left over file on the next start up? If, by wild chance, some other program really likes that name as well, you have a problem. A trully temp file name avoids these issues.

User is online!Profile CardPM
+Quote Post

Extas
RE: I Need To Find The Directory My Program Is Installed In.
18 Apr, 2008 - 08:37 AM
Post #5

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 9

now i see it your way, i think i'll change it, thanks for the heads up smile.gif



This post has been edited by Extas: 18 Apr, 2008 - 09:04 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:29PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month