繁体   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