简体   繁体   中英

File.Create(path) error VB.NET

Hi i've used the code bellow successfully at the beginning but i don't know what i did so it stopped creating the file MessageIO.dat under the folder (ProgramFiles)\\UniWin Activator Data

i used this code: (result: created only folder UniWin Activator Data )

Dim UniWinPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "UniWin Activator Data")
Directory.CreateDirectory(UniWinPath)

Dim MsgIO = Path.Combine(UniWinPath, "\MessageIO.dat")
File.Create(MsgIO)

and used this: (result: error at the command File.Create )

Dim UniWinPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "UniWin Activator Data\MessageIO.dat")
File.Create(UniWinPath)

and used this: (result: nothing happened)

Dim UniWinPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "UniWin Activator Data")
Dim MsgIO = Path.Combine(UniWinPath, "\MessageIO.dat")
File.Create(MsgIO)

what's the way to create that file? (I have admin rights already)

When combining paths, you should not specify the "\\" char at begining of the second path item as this will mean the root path! for example, Path.Combine("D:\\Folder1", "\\MessageIO.dat") will result "\\MessageIO.dat". but you have to write Path.Combine("D:\\Folder1", "MessageIO.dat") which will return "D:\\Folder1\\MessageIO.dat"

Note: in windows 7 or above, access to special folders like as Program Files require special permissions! check that your app has such permission. (you can test for other norman folder first to ensure other parts of your code is ok)

The first of your code is perfectly fine. Just change Dim MsgIO = Path.Combine(UniWinPath, "\\MessageIO.dat") to Dim MsgIO = Path.Combine(UniWinPath, "MessageIO.dat") . (Remove the backslash). Path.Combine automatically adds one. And as always, to access special directories, make sure you have Administrator Privilleges. The reason the last two codes aren't working is that File.Create creates a file in an existing directory. It can't create the directory itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM