简体   繁体   English

File.Create(路径)错误VB.NET

[英]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 嗨,我在一开始就成功使用了以下代码,但我不知道自己做了什么,因此它停止在(ProgramFiles)\\UniWin Activator Data文件夹下创建MessageIO.dat文件

i used this code: (result: created only folder UniWin Activator Data ) 我使用此代码:(结果:仅创建文件夹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 ) 并使用了它:(结果: File.Create命令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". 例如,Path.Combine(“ D:\\ Folder1”,“ \\ MessageIO.dat”)将产生“ \\ MessageIO.dat”。 but you have to write Path.Combine("D:\\Folder1", "MessageIO.dat") which will return "D:\\Folder1\\MessageIO.dat" 但是您必须编写Path.Combine(“ D:\\ Folder1”,“ MessageIO.dat”),它将返回“ D:\\ Folder1 \\ MessageIO.dat”

Note: in windows 7 or above, access to special folders like as Program Files require special permissions! 注意:在Windows 7或更高版本中,访问特殊文件夹(如程序文件)需要特殊权限! check that your app has such permission. 检查您的应用是否具有此类权限。 (you can test for other norman folder first to ensure other parts of your code is ok) (您可以先测试其他norman文件夹,以确保代码的其他部分正常)

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") . 只需将Dim MsgIO = Path.Combine(UniWinPath, "\\MessageIO.dat")更改为Dim MsgIO = Path.Combine(UniWinPath, "MessageIO.dat") (Remove the backslash). (删除反斜杠)。 Path.Combine automatically adds one. Path.Combine自动添加一个。 And as always, to access special directories, make sure you have Administrator Privilleges. 与往常一样,要访问特殊目录,请确保您具有Administrator Privilges。 The reason the last two codes aren't working is that File.Create creates a file in an existing directory. 后两个代码不起作用的原因是File.Create在现有目录中创建了一个文件。 It can't create the directory itself. 它无法创建目录本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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