Windows Dev Center

# How to create a hidden folder?

• ### Question

• Hi,
I want a code to create a hidden folder , Sub Folders , And files in the Folders
How can I do that
I usually use the following code to create a folder

If My.Computer.FileSystem.DirectoryExists("c:\Test\") = False Then
My.Computer.FileSystem.CreateDirectory("c:\Test\")
End If

And this code for Files

If My.Computer.FileSystem.FileExists("C:\Test\text.txt") Then
System.IO.File.WriteAllText("C:\Test\text.txt") , Text.Text)
End If
Is there any way to make them hidden?

Thanks
Thursday, January 7, 2010 4:29 PM

• Hi,

You can try something like this :

Dim myHiddenDir As DirectoryInfo = Directory.CreateDirectory("C:\HiddenDir")
myHiddenDir.Attributes = (FileAttributes.Directory Or FileAttributes.Hidden)
Dim myFile As StreamWriter = New StreamWriter("C:\HiddenDir\myFile.txt")
myFile.Write("Test")
myFile.Close

Hope this helps,

Mathieu
Mathieu Francesch Sharplog Engineering http://www.sharplog.fr
• Proposed as answer by Thursday, January 7, 2010 5:36 PM
• Marked as answer by Sunday, January 10, 2010 1:42 AM
Thursday, January 7, 2010 4:43 PM
• Just set the Attributes property on the files in the folder, in the same way that your setting the property for the folder.  You will probably want to access the file's existing attributes first, and add the hidden attribute rather than just overwriting all the attributes values.

For Each fname In System.IO.Directory.GetFiles("C:\Test", "*.txt")

Dim fi As System.IO.FileInfo = New System.IO.FileInfo(fname)
System.IO.File.SetAttributes(fname, fi.Attributes
Or System.IO.FileAttributes.Hidden)

Next

• Marked as answer by Sunday, January 10, 2010 1:42 AM
Sunday, January 10, 2010 12:21 AM

### All replies

• Hi,

You can try something like this :

Dim myHiddenDir As DirectoryInfo = Directory.CreateDirectory("C:\HiddenDir")
myHiddenDir.Attributes = (FileAttributes.Directory Or FileAttributes.Hidden)
Dim myFile As StreamWriter = New StreamWriter("C:\HiddenDir\myFile.txt")
myFile.Write("Test")
myFile.Close

Hope this helps,

Mathieu
Mathieu Francesch Sharplog Engineering http://www.sharplog.fr
• Proposed as answer by Thursday, January 7, 2010 5:36 PM
• Marked as answer by Sunday, January 10, 2010 1:42 AM
Thursday, January 7, 2010 4:43 PM
• Hi,
sorry for being late but I got exams these days
However, I tried this code it works for the folder but the files are not working
it create the folder hidden but when it create the file it is visible
Actually I was thinking about changing the attributes it will be better because by this code I want to change the attributes of existing files and folders I just put the create code for test only
Thanks.
Saturday, January 9, 2010 9:54 PM
• Just set the Attributes property on the files in the folder, in the same way that your setting the property for the folder.  You will probably want to access the file's existing attributes first, and add the hidden attribute rather than just overwriting all the attributes values.

For Each fname In System.IO.Directory.GetFiles("C:\Test", "*.txt")

Dim fi As System.IO.FileInfo = New System.IO.FileInfo(fname)
System.IO.File.SetAttributes(fname, fi.Attributes
Or System.IO.FileAttributes.Hidden)

Next

• Marked as answer by Sunday, January 10, 2010 1:42 AM
Sunday, January 10, 2010 12:21 AM
• Thanks all
It works
Thanks
Sunday, January 10, 2010 1:42 AM