|
-
Feb 8th, 2012, 09:24 PM
#1
Thread Starter
New Member
Hello and beginner help
First off I'd like to say hello and I'm looking forward to learning from everyone here. I'm very new to VB and I'm already stuck. Thanks in advance!
Now on to my problem.
I had to put together a simple app to move files from one folder to another with a one button app and some text intructions on the main form. No sweat and it sends a message box at the end. BUT! now I have to make a similar app and copy all the files from a my documents folder to a network drive. All of a sudden I'm stuck! How do I make this work for more than one person and they login to a domain? I want to use the following but what would be the variable? It seemed so easy!
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\%username%\My Documents", "U:\Backup", True)
-
Feb 9th, 2012, 01:35 AM
#2
Re: Hello and beginner help
For future reference, please provide meaningful thread titles that describe the topic of the thread. Otherwise we have to open every single thread to find those that might be relevant to us, which can be a big time waster and may turn people off viewing your thread.
As for the question, you generally cannot just include an environment variable in a literal string an expect it to be interpreted correctly. What you can do is call Environment.ExpandEnvironmentVariables and pass your string to it, then use the result of that, which will contain the resolved values of the environment variables:
Code:
My.Computer.FileSystem.CopyDirectory(Environment.ExpandEnvironmentVariables("C:\Documents and Settings\%username%\My Documents"), "U:\Backup", True)
In this case though, that's a very bad idea. There are various reasons that that path could be incorrect:
1. The user is not using Windows XP, i.e. Vista and Win 7 use a different default path.
2. The user has installed Windows on other than C: drive.
3. The user has moved their document store to a different location, e.g. I always move mine to a secondary hard drive so that it will be unaffected if I ever have to reformat the system drive and reinstall Windows.
You should always use standard methods for getting standard folder paths, which means either Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories. That will give you the correct path for folders like user documents no matter where they are because it queries Windows itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|