创建文件导致vb.net中的访问被拒绝

时间:2018-07-16 05:31:33

标签: 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
    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

我不知道如何授予允许创建文件的特定文件夹的权限。

0 个答案:

没有答案