在vb.net中创建文件导致访问被拒绝
我正在尝试创建一个函数,该函数将在特定文件夹中创建一个文件。但每次我试图创建文件时,它都会显示拒绝访问。我读了很多解答,但我仍然不知道如何解决这些问题在vb.net中创建文件导致访问被拒绝,vb.net,file,permissions,access-denied,Vb.net,File,Permissions,Access Denied,我正在尝试创建一个函数,该函数将在特定文件夹中创建一个文件。但每次我试图创建文件时,它都会显示拒绝访问。我读了很多解答,但我仍然不知道如何解决这些问题 Dim x as string x = Path.GetDirectoryName(Application.ExecutablePath) Private Sub Button1_Click(sender As Object, e As EventArgs) Handles APIsaveBtn.Click SaveFileDi
Dim x as string
x = Path.GetDirectoryName(Application.ExecutablePath)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles APIsaveBtn.Click
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*|CSV Files (*.csv)|*.csv"
SaveFileDialog1.FileName = ""
Dim newArray As New ArrayList()
Dim path As String = x
If File.Exists(path) = True Then
System.IO.File.WriteAllText(path, "")
newArray.Sort()
My.Settings.API_Path = path
My.Settings.API_Key = APIkeyTxtBox.Text
Using sw As StreamWriter = New StreamWriter(path, True)
sw.WriteLine(APIkeyTxtBox.Text)
End Using
apikey = APIkeyTxtBox.Text
MessageBox.Show("Save Sucessfully", "Save")
ElseIf File.Exists(path) = False Then
Dim newFile As IO.StreamWriter = System.IO.File.CreateText(path)
newFile.Close()
My.Settings.API_Path = path
My.Settings.API_Key = APIkeyTxtBox.Text
Using sw As StreamWriter = New StreamWriter(path, True)
sw.WriteLine(APIkeyTxtBox.Text)
End Using
apikey = APIkeyTxtBox.Text
MessageBox.Show("Save Sucessfully", "Save")
End If
我不知道如何授予权限以允许创建特定文件夹的文件。您的
路径
似乎是目录路径,而不是文件路径。@Bob文件路径和目录路径c:\temp
之间的区别是目录路径。您可以在其中创建文件,但不能使用流在其中写入。。。因为您无法在目录中写入文本<代码>c:\temp\myfile.txt是一个文件路径。您可以用这个地址创建一个文件,或者使用一个流在其中写入。@Bob如果我想在C:\temp中创建一个文件怎么办?有什么方法可以做到这一点吗?你必须知道你的文件名。因此,假设您的文件夹(c:\temp
)有path
),文件(Myfile.txt
)有filePath
),完整路径将是filePath=System.Io.path.Combine(path,fileName)
,然后您只需执行System.Io.file.CreateText(filePath)
。