Directory.GetDirectories on %USERPROFILE%\My Documents - vb.net

I'm creating a UserControl for my program which will mimic the OpenFileDialog.
When I navigate to a Directory I am using
System.IO.Directory.GetDirectories(Path)
and
System.IO.Directory.GetFiles(Path)
to obtain the contents of the Directory.
When I navigate to %USERPROFILE% (In my case it is C:\Users*UserName*) and there is a hidden My Documents folder as well as a Documents folder.
When you navigate to the My Documents folder it takes you to the Documents folder, but it appears in the list from
System.IO.Directory.GetDirectories(Path)
My question is, is there a way, in code, to find where the My Documents folder is pointing to?
I know that I can find where the My Documents folder is by using
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
but I need to find the actual path each directory will save to in case it's different than it's actual address.
I'd be open to alternatives if it isn't possible.
Thanks

Related

I want to copy the file in sandbox without NSOpenPanel

I want to copy some files to specified folder by NSOpemPanel.
The source file is reading from XML and show to list in NSTable.
I can copy file by copyItemAtPath.
But now my app will turn to sandbox, then I can’t copy the file by copyItemAtURL.
How do I copy the file in sandbox mode?
I was looked over a lot of post. And I think the Security-scoped Bookmark may be a solution for this.
But I can’t create "Security-scoped Bookmark" from XML inside the path (the path was convert to NSURL ready).
I was setting to sandbox.entitlements but it's not clear this problem.
Is there any way for this?
Develop in macOS10.12 and Xcode8.3.3
Thanks
How do I copy the file in sandbox mode?
It is unclear what your current code is doing, but the rules under the sandbox are simple: To read or write a file located outside of an application's own container (which is hidden away under the Library folder) your application must either:
Use NSOpenPanel to obtain a URL from the user for the file path; or
Use NSOpenPanel to obtain a URL from the user for one of the ancestor folders of the file.
The second option gives access to a whole folder, including any sub-folders; i.e. the whole file/folder subtree rooted in the folder.
As you want to copy "some files" it sounds like asking the user for permission for the folder is appropriate. You can customise the NSOpenPanel to be a "request permission" dialog. If you are requesting a specific folder you can also have the dialog open in it's containing folder and only have the specific folder enabled for selection by the user.
Once you have the URL for the folder from NSOpenPanel you can create a security scoped bookmark for it and save that in your app's preferences or other configuration file (stored within the app's container). Doing this enables your app to regain access to the folder on subsequent executions without asking the user again.
If after investigating this issue and writing some code you hit an issue ask a new question, showing your code, and explaining the problem. Someone will undoubtedly help you with the next step.
HTH

How do I use the folder path that a user selects and use that path when they download files?

I'm in the process of creating a launcher for a game with Visual Basic and I have pretty much everything done except many people may have files in different drives. So, I want them to select a file path then when they have selected that path I want the download button to download the files that the need downloaded to the selected directory. I was thinking that the beginning of the code would be something like this:
Form2.TextBox1.Text
and then something else after that in the buttons code.
I hope I'm being specific enough.
P.S. The reason why this is an issue is because if I use System.Diagnostic.Process.Start("C:\Program Files (x86)\[remaining folderpath here]") to launch the .exe then the program will only look for the files in that directory even though some people may have the files in their D: drives, etc. And, I have a FolderDialog setup and working.

Select a file and get its path and filename

Using VS 2013 and vb.
I have a web page and on a button click i would like file explorer to open so the user can navigate to a folder location and highlight a file. The web application will then get the path and filename of the file. Just to be clear I dont want to open the file I just want the details.
I cant seem to find a way of doing this, can you help?
UPDATE
Apologies perhaps I havent explained my self very well.
This is so a user can reference a document saved on a file share with a particular audit (the web page is for recording audits). Therefore they will click a button and file explorer will open. They will navigate to a folder (will be different everytime) and select a file. File explorer will then close and the web application will know the path of the file and its name. I can then use this to create a link on the web page to that document.
For getting directory path use; System.IO.Path.GetDirectoryName(path)
For file name System.IO.Path.GetFileName(path) or System.IO.Path.GetFileNameWithoutExtension(path)
more info is available at http://www.dotnetperls.com/path
Use path.getFullPath() method to get the file path as a string.
If you want to get the file name only use path.getFileName() method.

Open a specific file in vb.net

I want my program to open a specific .txt file. The text file will always stay in the same folder within the solution folder. However, the location of the solution folder itself may change if the solution is moved to a different computer, or a different directory on the current computer.
I know how to hardcode the file path append the file name and then open it. But how can I define the file path so that the file can still be opened if the solution moves to a different computer?
If the file is contained within the solution, you can use a virtual path which is then mapped to a physical path using Server.MapPath
The following should work :
Dim filePath As String = Server.MapPath("~/FileName.txt")
Please note that the location of FileName.txt in my example is in the root of the solution and not in any specified folders, ~/ is essentially the root of the current solution.
For more information on the Server.MapPath method and Virtual Paths see below:
Server.MapPath MSDN Documentation
Virtual Path Utility Class
If you are sure that your application (.exe) and (.txt) files are in the same folder, then just use the file name - do not put the path.
IO.File.OpenText("thefile.txt")
Edited:
Mostly "current working directory" is the same directory where the (.exe) file exists. Yes, sometimes, it is not the same directory. Thus, Application.ExecutablePath would be the right solution.
Dim fn As String
fn = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\")+1) & "thefile.txt"
IO.File.OpenText("thefile.txt")
...
If you meant to say your FileName.txt was always present in the same directory as the assembly, you could do something simple like this:
Function GetAssemblyDirectoryPath() As String
Dim fullAssemblyPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Return fullAssemblyPath.Substring(0, fullAssemblyPath.LastIndexOf("\"c))
End Function
This just returns the path to the folder where the assembly resides. The "Solution" might not always be present, but the assembly will, so this will work in both cases (if you make sure the file is always copied to the output directory).
If you're asking about a desktop application, application.ExecutablePath will do what you want. It's not really a good idea though, if your application will reside within Program Files - it's best to avoid writing to anywhere within there, and you will have to run as administrator on post-XP OS's .

Vb. how to make the file save files in a folder iam using the files in form

Lets Says I Want To Make A Think Like Saver Forum But Another Think Like
Folder Browser Diadlog Download / Save Files I Use In The Forum Not Load Then Save
Files I Use It And In The Folder They Select (Another Pc) I Want It To Save The Files/Download In The Folder And They Will Be Think Named Wolfteam.exe Thats Mean Start The Game And Use The Files In The Game Folder
Thanks For Help.