簡體   English   中英

vb.net訪問文件夾被拒絕

[英]vb.net access to folder denied

我到處搜索此錯誤,更改程序的清單文件以管理員身份運行,但未做任何更改,我為自己創建了一個程序以獲取視頻流鏈接,並將其第一部分放在文本框1中,第二部分放在文本框中。 textbox2這些放在一起,並添加了劇集的編號,但是當我嘗試保存帶有所有鏈接的txt文件時,由於訪問被拒絕,我無法保存它。

Imports System
Imports System.IO
Imports System.Text

Public Class Form1
    Dim l1 As String
    Dim l2 As String
    Dim ep As Integer
    Dim nEp As Integer
    Dim testo As String
    Dim path As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label4.Text = "Link:" & vbCrLf
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        l1 = CStr(TextBox1.Text)
        l2 = CStr(TextBox2.Text)
        nEp = CInt(TextBox3.Text)

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""

        If l1 <> "" And l2 <> "" And IsNumeric(nEp) Then
            If ep <= 9 Then
                For ep = 0 To 9
                    Label4.Text = Label4.Text + l1 & "0" & ep & l2 & vbCrLf
                Next
                If ep > 9 Then
                    For ep = 10 To nEp
                        Label4.Text = Label4.Text + l1 & ep & l2 & vbCrLf
                    Next
                    testo = Label4.Text
                    FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory
                    If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                        path = FolderBrowserDialog1.SelectedPath
                    End If
                    File.Create(FolderBrowserDialog1.SelectedPath).Dispose()
                    File.WriteAllText(FolderBrowserDialog1.SelectedPath, testo)
                End If
                End If
        Else
            MsgBox("Inserisci i dati correttamente!")
        End If


    End Sub

End Class

我設法使其工作,但如果我想再次使用它而不關閉表格,則該按鈕不會執行任何操作。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    l1 = CStr(TextBox1.Text)
    l2 = CStr(TextBox2.Text)
    nEp = CInt(TextBox3.Text)
    nomeFile = CStr(TextBox4.Text)

    If l1 <> "" And l2 <> "" And IsNumeric(nEp) Then
        If nomeFile <> "" Then
            If ep <= 9 Then
                For ep = 0 To 9
                    Label4.Text = Label4.Text + l1 & "0" & ep & l2 & vbCrLf
                Next
                If ep > 9 Then
                    For ep = 10 To nEp
                        Label4.Text = Label4.Text + l1 & ep & l2 & vbCrLf
                    Next
                    testo = Label4.Text
                    If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                        path = FolderBrowserDialog1.SelectedPath + "\" + nomeFile + ".txt"
                    End If
                    File.Create(path).Dispose()
                    File.WriteAllText(path, testo)
                    MsgBox("File created")

                    TextBox1.Text = ""
                    TextBox2.Text = ""
                    TextBox3.Text = ""
                    TextBox4.Text = ""
                    Label4.Text = ""
                End If
            End If
        Else
            MsgBox("File name missing")
        End If
    Else
        MsgBox("You need to fill the requested inputs!")
    End If


End Sub

我看到的最大的事情是使用SaveFileDialog而不是FolderBrowserDialog 但是,您還可以進行更多清理:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ep As Integer 
    If String.IsNullOrWhitespace(TextBox1.Text) OrElse 
       String.IsNullOrWhitespace(TextBox2.Text) OrElse
       Not Integer.TryParse(TextBox3.Text, ep) Then

        MsgBox("You need to fill the requested inputs!")
        Exit Sub
    End If

    Dim sfd As New SaveFileDialog()
    sfd.InitialDirectory = Environment.SpecialFolder.DesktopDirectory
    If sfd.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub

    Dim names() As String = 
        Enumerable.Range(1, ep).
            Select(Function(e) String.Format("{0}{1:00}{2}{3}", TextBox1.Text, e, TextBox2.Text, vbCrLf)).
            ToArray()
    Dim result As String = String.Join("", names)

    Label4.Text &= result
    File.WriteAllText(sfd.FileName, Label4.Text)

    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
End Sub

該方法的主要內容是以下代碼:

Enumerable.Range(1, ep).
    Select(Function(e) String.Format("{0}{1:00}{2}{3}", TextBox1.Text, e, TextBox2.Text, vbCrLf)).
    ToArray()

它使用Enumerable.Range()函數生成一個整數序列,該整數序列的范圍是1到之前從TextBox3解析為ep變量的情節數。 然后,它使用IEnumerable<T>.Select()函數創建這些數字到所需字符串的投影。 Select()接受委托參數,該參數在這里作為lambda expression提供 此lambda表達式使用String.Format()將每個字符串放在一起。 具體來說,劇集編號放在{1:00}占位符中,其中:00部分是一個格式字符串,以確保至少兩位數。 然后,我們調用.ToArray()將其全部匯總到與String.Join()兼容的結構中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM