Showing the File Extensions inside the folder - vb.net

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.

Related

Delete directory on network shared folder

This question is bring from another forum which not having answer yet for my situation.
I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:
Check destination (network shared folder) path is empty or not.
Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
Copy folder content from source to new destination.
No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?
Delete directory code that I use but exception "Could not complete operation since directory is a root directory":
My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)
Please assist
EDITED:
To delete all files inside the main directory:-
Dim directory As New DirectoryInfo(strDestination)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next file
To delete all folders inside the main directory:-
For Each folder As DirectoryInfo In directory.GetDirectories()
folder.Delete(True)
Next folder
Use this instead (it's C#, you'll need to convert it to VB.NET):
DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
file.Delete();
}

How to get path from VB class?

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)

Download to root directory

Okay, so I have been searching for ages to find this but no luck.
I am using:
Me.downloader.DownloadFileAsync(New Uri(fileUrl), Path.GetFileName(fileUrl), Stopwatch.StartNew)
To download a file but I want it to save to the root directory of my program in a file called launcher.
So for example, if my program is on the desktop and I open it and click start I want it to create the launcher folder if it's missing then download the files into that and if it's not then just download the files into it.
I've been looking everywhere to find code which would allow me to do this and I have tried lots of different things.
At the moment, it justs saves in the root directory of where the program is.
Thanks.
Try something like this:
Dim baseDir As String = AppDomain.CurrentDomain.BaseDirectory
Dim launcherDir As String = Path.Combine(baseDir, "Launcher")
If Not Directory.Exists(launcherDir) Then
Directory.CreateDirectory(launcherDir)
End If
Dim targetFile = Path.Combine(launcherDir, Path.GetFileName(fileUrl))
Me.downloader.DownloadFileAsync(New Uri(fileUrl), targetFile, Stopwatch.StartNew)

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)

Protect single file!

I'm trying to protect a folder and the files inside it.
I'm able to protect the folder itself, so that if somebody clicks on it he will get a message:
"You don't currently have permission to access this folder!"
But I can still access files in that folder. For example, if somebody knows the name of a file inside the folder he can type D:\ProtectedFolder\pdffile.pdf and he can open the file!
So, my question is:
Can I protect single files inside the folder?
This is the function that I use for folder lock:
Public Function Lock(ByVal folder As
String, ByVal user As String)
Dim FilePath As String = folder
Dim fs As FileSystemSecurity = File.GetAccessControl(FilePath)
fs.AddAccessRule(New FileSystemAccessRule(user,
FileSystemRights.ListDirectory,
AccessControlType.Deny))
fs.AddAccessRule(New FileSystemAccessRule(user,
FileSystemRights.FullControl,
AccessControlType.Deny))
File.SetAccessControl(FilePath, fs)
Return 0
End Function
Thanks!
You will also have to deny FileSystemRights.Read if you want to prevent that. And technically you have to make sure that the files inherited their rights from the folder.
Specify FileShare.None for File.Open. You can see my C# implementation of it here with full source code. Convert it to VB.NET if you'd like.
This is the message you receive when trying to open a file locked by the application:
I think that's what you're after.