change default directory for input/output
change default directory for input/output
change default directory for input/output
change default directory for input/output
change default directory for input/output
change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output
change default directory for input/output change default directory for input/output
change default directory for input/output
Go Back  Xtreme Visual Basic Talk > > > change default directory for input/output


Reply
 
Thread Tools Display Modes
  #1  
Old 04-06-2005, 02:53 AM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default change default directory for input/output


I'm coding a program that reads/writes from several text files as data files. No matter what I do when I run the program to debug it defaults to the My Documents folder to look for files. Is there a way I can change this to the directory the executable is in without putting in a specific path into the code? I want this to work on any system it is installed on regardless of what directory it is in. BIG Thanks in advance
Reply With Quote
  #2  
Old 04-06-2005, 07:14 AM
ZooTV's Avatar
ZooTV ZooTV is offline
Junior Contributor
 
Join Date: Jun 2004
Location: Venezuela
Posts: 265
Default

I think that you can use Application.StartupPath in that situation. I don't know any details on how you're doing it, so I can't help you more, but I hope that solves it.
__________________
ZooTV - Inferi
Reply With Quote
  #3  
Old 04-06-2005, 10:58 AM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default

I think thats it, thank you very much!
Reply With Quote
  #4  
Old 04-06-2005, 02:30 PM
AtmaWeapon's Avatar
AtmaWeaponchange default directory for input/output AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

If you specify an absolute path rather than a relative path, you will never have any problems with this.

Usually default I/O happens in Application.StartupPath, so I will assume you are having problems with OpenFileDialog and SaveFileDialog. Both have an InitialDirectory property that specifies where the dialog should default to.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 04-06-2005, 10:31 PM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default

The dialogs weren't where I was having a problem, those have properties that can be set well enough. I was hoping for a smarter functionality when the program opened files through filestreams with no pathname, but they went to My Documents first too. Silly me thought they would default to the directory the executable was in.

But Application.StartupPath was the answer to my problem, everything has been tweaked and is working properly.

Just out of curiosity by "absolute path" do you mean write out the path, eg. "C:\I\WRITE\BAD\CODE\donti.now"?
Reply With Quote
  #6  
Old 04-07-2005, 05:19 AM
ZooTV's Avatar
ZooTV ZooTV is offline
Junior Contributor
 
Join Date: Jun 2004
Location: Venezuela
Posts: 265
Default

yes, absolute path doesn't change, unlike relative paths, that depend on the system installation folders, languages, etc.
__________________
ZooTV - Inferi
Reply With Quote
  #7  
Old 04-08-2005, 12:19 AM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default

but then how do you recommend handling distribution?
Reply With Quote
  #8  
Old 04-08-2005, 08:22 AM
AtmaWeapon's Avatar
AtmaWeaponchange default directory for input/output AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

That is why you have things like Application.StartupPath and Environment.GetFolderPath, and why it is foolish to alter the StartupPath variable.

For the purposes of this example, %PROGRAMROOT% will stand for the path to your executable (i.e. C:\Program Files\Your Program\Your Program.exe)

Suppose you have a directory located within your program directory, %PROGRAMROOT%\data. You can use an absolute path to access it by building the path like so:
Code:
dataPath = System.IO.Path.Combine(Application.StartupPath, "data")
For folders such as "My Documents", the path is given from Environment.GetFolderPath(). There are several arguments to this function, the one that specifies My Documents is "Personal".

Additionally, the Application class has UserAppDataPath and CommonAppDataPath properties that point to directories in either the user's or the "All User"'s Application Data directory. I believe these properties even tack on your program's name to the end of the path, though you still need to make sure those directories are created.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #9  
Old 04-10-2005, 10:37 PM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default

Which is exactly my point, Application.StartupPath has been my solution. I just find it odd that I've posted this question in many forums, and almost everyone has suggested using "explicit" or "absolute" pathnames. Is Visual Basic that ill-regarded a language that nobody thinks of distribution when coding with it?
Reply With Quote
  #10  
Old 04-11-2005, 04:51 PM
MKoslof's Avatar
MKoslofchange default directory for input/output MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

I am not sure that I follow. A program, no matter what language, needs to know where to look for source components or anything for that matter. If you want to put your components in the same directory or structure as your application, you should be using Application.StartupPath or Environment.CurrentDirectory. If you want to use Windows default paths, you can use the EnvironmentVariables method such as:

ie - the logged in user's temp directory

Code:
Environment.GetEnvironmentVariable("Temp")

If you don't want to use either option, then you need to use an xml file, an ini file, a database table, or something that your application can read to get the directory of your components. These are basically your only logical options besides hard-coding the location
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #11  
Old 04-11-2005, 07:43 PM
AtmaWeapon's Avatar
AtmaWeaponchange default directory for input/output AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by abno
Which is exactly my point, Application.StartupPath has been my solution. I just find it odd that I've posted this question in many forums, and almost everyone has suggested using "explicit" or "absolute" pathnames. Is Visual Basic that ill-regarded a language that nobody thinks of distribution when coding with it?
Your default input/output path will be the directory of the executable. If you plop your .exe at "C:\Program Files\Application", and you declare code like so:
Code:
Dim fs as New StreamWriter("test.txt")
fs.WriteLine("Some people like to make life")
fs.Close()
You will get a file "C:\Program Files\Application\test.txt". The path I just listed is absolute; the file was declared with the relative path "test.txt".

Now, this isn't always true. Various I/O functions can alter the working directory. Suppose the working directory has been altered to "C:\Documents and Settings\wonky\My Documents" and the above code is run. Your file would be written at "C:\Documents and Settings\wonky\My Documents\test.txt".

What if the user changes the working directory again? What if after the working directory is changed you try to open the file using the relative path "test.txt"? It is likely the file will not be at the current location, and if it is it is not the version you want. For this reason, relative paths are a horrible idea for your I/O functions.

Absolute paths are fixed, though, so what if the user installs the program in some stupid directory like "D:\my folder\aaaaa\bbbbb\ddddd\"? If you had hard-coded "C:\Program Files\Application", your app would bomb. So how the devil do you use absolute paths?

You build them using special variables. Application.StartupPath ALWAYS returns the current path to your program. So on your PC Application.StartupPath will be "C:\Program Files\Application", and on the odd PC it will be "D:\my folder\aaaaa\bbbbb\ddddd\". What if you want to save it in the user's My Documents folder? You'd get that path from Environment.GetFolderPath(Environment.SpecialFolder.Personal).

So a more proper way to write the code snippet I used would be:
Code:
Dim filePath as String
filePath = IO.Path.Combine(Application.StartupPath, "test.txt")
Dim fs as New StreamWriter(filePath)
fs.WriteLine("a little tougher than it is")
fs.close
Though the code is functionally equivalent, you have guaranteed that you are using the right path to the file.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #12  
Old 04-16-2005, 11:14 AM
abno abno is offline
Newcomer
 
Join Date: Apr 2005
Posts: 17
Default

I perfectly understand the workings of Application.Startup path, thank you all. But my point was that so many people have recommended using explicit paths which would make my program useless to anyone but myself. It was the only advice on other forums and even came up here once. It seems that VB programmers have little regard for this language.
Reply With Quote
  #13  
Old 04-16-2005, 01:23 PM
PWNettlechange default directory for input/output PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

Quote:
Originally Posted by abno
I just find it odd that I've posted this question in many forums, and almost everyone has suggested using "explicit" or "absolute" pathnames.
In my experience at this forum it'd be pretty rare for anyone to suggest the use absolute path names for most I/O use. In most cases folks here will suggest relative pathing solutions, as you've seen in this thread.

Quote:
Originally Posted by abno
But my point was that so many people have recommended using explicit paths which would make my program useless to anyone but myself.
Most folks that have actually distributed an application know better! The world wide web is huge and nobody is obligated to publish factual information, so who knows what you're reading at other forums or sites, but I think 99+% of the time you'll get good advice at this site. Misinformation is usually commented on, although in any large forum it's still possible for inaccuracies to slip by once in a while.

Over time there have been many discussions here about relative pathing issues - usually after someone has found out the hard way about paths when distributing.

It's also possible that people have misinterpreted what you wanted to do and/or didn't clearly state their solutions for you.

Quote:
Originally Posted by abno
It seems that VB programmers have little regard for this language.
Actually, I think most VB coders are generally passionate (in a positive way) about VB. VB is just one of many programming languages. Poor coding and/or distribution practices can occur regardless of language.

Cheers,
Paul
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
change default directory for input/output
change default directory for input/output
change default directory for input/output change default directory for input/output
change default directory for input/output
change default directory for input/output
change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output change default directory for input/output
change default directory for input/output
change default directory for input/output
 
change default directory for input/output
change default directory for input/output
 
-->