How to get path from VB class? - vb.net

How to get file path within VB class - assuming our VB class in located in code behind file of ASP.net webpage?
e.g. We are viewing ASP.net webpage ( http://localhost:1253/website/web-page.aspx ) - and our VB class in located in web-page.aspx.vb file.
Public Class FileLocation
Public Function GetFileLocation() As String
Dim location as string = '
// get "c:/intenpub/website/file.jpg" when only filename "file.jpg" is known
Return location
End Function
End Class

anYou can get the location of a known file, say if you know that "file.jpg" is in the website's root...
My.Application.Info.DirectoryPath
'Returns the directory path of an application - there are ones for web stuff too
and you can check to see if a file exists in a specific known location...
If System.IO.File.Exists(My.Application.Info.DirectoryPath + "\file.jpg") Then
'Do Something
End If
But you can't just easily get a location of a file named "whatever" without searching the entire directory, which may include several results and you wouldn't know which is the correct one....

If you are trying to find a file by name in a specific directory or a directory and its subdirectories, you can use a built in .NET function:
Imports System.IO
Directory.GetFiles("c:/intenpub/website/", "file.jpg", SearchOption.TopDirectoryOnly)
Directory.GetFiles("c:/intenpub/website/", "file.jpg", SearchOption.AllDirectories)

Related

Showing the File Extensions inside the folder

Currently, I am working on a feature that will make the files inside the folder that will not hide the file extensions using this code.
Imports Microsoft.Win32
Sub SetNoDrives(value As Integer, path as string)
Dim RegPath As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]"
Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey(RegPath)
Key.SetValue("HideFileExt", value, RegistryValueKind.DWord)
End Using
End Sub
the problem is, I don't know where to place the string path (folder path) on the code. the path is the specific location where you will always show the file extensions of the files inside the folder.
Any help will be much appreciated.
The reason you don't know where to put the folder path is because there is nowhere to put the folder path. This is a user-wide option, i.e. either extensions are displayed in every folder or no folder for the current user. You don't get to choose on a folder by folder basis. At least, there is no option in Windows/File Explorer to do that and I've never seen mention of it being possible, even when specifically searching for it.

Grabbing current user.config path (Visual Basic)

Is there a way of grabbing the location of the My.Settings user.config location? So for example I want to be able in VB to grab the path of the user.cofing file path to a string
The reason I ask is that I have an application where the user.config file is backed up and then restored, the issue is that with the my.settings folder structure it uses a unique hash with the folder name meaning that I cannot write into the code a static folder path, instead I need to be able to grab the location of the user.config OR be able to get the folder name of the application AppData.
Any ideas?
To put this in perspective, currently I'm using something like this:
Dim filePath As String
filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\My_App\My_App.exe_Url_<the_hash_that_changes_causing_issues>\1.0.0.0\user.config"
Because of the hash change this will not always work
Try executing this code:
Dim mainConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim userConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
Dim userLocalConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
Console.WriteLine(mainConfig.FilePath)
Console.WriteLine(userConfig.FilePath)
Console.WriteLine(userLocalConfig.FilePath)
You'll need to reference System.Configuration.dll and import System.Configuration.

How can I get the names a files inside a directory

Good day all
I have a test path name ""C:\test". There are the following folders in my directory:
importantStuff
UselessStuff
TopSecret
My question is this: How can I have Visual Basic return these exact names if I input the path "C:\test"? I have tried to use Directory.GetFiles(path), but it returns the path of those folders, and not their names. I'm stuck with this.
Just as a note: I am trying to port a program I wrote in python to vb.net. In python there is the function os.listdir(path). I essentialy want the vb.net equivalent of this
Try this
Dim direct As New DirectoryInfo(".\")
'Get the files based on .txt extension
Dim files As FileInfo() = direct.GetFiles("*.*")
'loop through each files and add it to Listbox control
For Each file As FileInfo In files
ComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name))
Next

How do I get resource file values in Visual Basic?

I'm new to Visual Basic, and I'm having issues accessing the resource file for my project.
Dim rm As Resources.ResourceManager = New Resources.ResourceManager("MyProjectName.My.Resources.Resources", [Assembly].GetExecutingAssembly())
Dim myValue = rm.GetString(lookUpKey) 'boom Object reference not set to an instance of an object.
I think the issue is with the string "MyProjectName.My.Resources.Resources".
Would I be better off moving the strings into their own resource file?
I thought it was something similar to:
my.Resource.whateverhere
Is that not the kind of resources you are looking for?
Try
Global.<MyNamespace>.My.Resources.<ResourceStringName>
to access Resource Strings
Try ResourceManager("MyProjectName.Resources", ...), otherwise if it's the application resources you can simply use My.Resources.HD (see here:My.Resources Object)
or
Open Reflector, load your assembly there, go to resources, a list of resources appears, search for the one containg 'HD', copy the name (it's like MyProjectName.Resources.resources), remove the last .resources and try with that.
Refer to the MSDN article Retrieving Resources with the ResourceManager Class for naming convetions:
Dim myManager As New _
System.Resources.ResourceManager("ResourceNamespace.myResources", _
myAssembly)
If you load an external .resx file and want it to show up under intellisense My.Resources then you need to do 2 things.
First the file should be in the root of your project. Simply right click the project and do "Add Existing Item", and give it your .resx file. You should notice that there is no chevron to expand the resx file like your built-in resource file.
The last step is to highlight your resx file and go to the properties window. Under Custom Tool put "ResXFileCodeGenerator" and under the Custom Tool NameSpace put "My.Resources". You should now be able to programmatically access this resource under My.Resources.[name of the resx file].resource_item.
I was unable to access the resource file until I moved the .resx file into its own project and referenced that project from my main one. I also had to create a dummy class in that project so that it would compile into a DLL file.
The code for accessing the resource file is actually located in the generated Resource.resx.vb file.
I was able to access the resource file using the following code.
'Name of Class Library where I moved the resx file
Dim classLibraryName As String = "ResourceProj"
'Name of Resource File without the .resx suffix
Dim resourceFileName As String = "Mappings"
'Finding the assembly of the resx file, ResourceProjClass is a dummy class I created so that the dll would build.
Dim myAssembly As Assembly = GetType(ResourceProj.ResourceProjClass).Assembly
Dim rm As Resources.ResourceManager = Nothing
rm = New Resources.ResourceManager(classLibraryName & "." & resourceFileName, GetType(myAssembly)
Return rm.GetString(lookUpKey)
Simply:
Dim loginMessage As String = Global.Resources.NameOfYourResxFile.NameOFVariable

Making folder and copying file

I want my application to look for a file in drive C's specific folder (say C:\myFolder\abc.mdb), if found just give a message if not, make the folder in drive C:\ and then copy the file.
How to do this?
Thanks
Furqan
You could use the File, Directory, and Path objects in the System.IO as shown below:
Imports System.IO
...
Dim path As String = "C:\myFolder\abc.mdb"
If File.Exists(path) Then
'TODO write code to create message'
Else
Dim folder As String = Path.GetDirectoryName(path)
If Not Directory.Exists(folder) then
Directory.CreateDirectory(folder)
End If
'TODO code to copy file from current location to the newly created directory path'
'i.e. File.Copy(FileToCopy, NewCopy)'
End If
I know this post is kind of old. I just wanted to update.
The quickest shortcut that will also create the Destination directory structure and in one line. Use Computer.FileSystem.CopyFile instead of System.IO.
My.Computer.FileSystem.CopyFile(sSourcefile, sDestinationfile)