我到处搜索该错误,将程序的清单文件更改为以管理员身份运行,但未做任何更改,我为自己创建了一个程序以获取视频流链接,并将其第一部分放在文本框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
答案 0 :(得分:0)
我设法使其正常运行,但是如果我想在不关闭表格的情况下再次使用它,则该按钮不会执行任何操作。
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
答案 1 :(得分:0)
我看到的最重要的事情是使用 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到之前从ep
解析到TextBox3
变量中的情节数目。然后,它使用IEnumerable<T>.Select()
函数创建这些数字到所需字符串的投影。 Select()
接受一个delegate参数,该参数在此处作为lambda expression提供。此lambda表达式使用String.Format()
将每个字符串放在一起。具体来说,情节编号位于{1:00}
占位符中,其中:00
部分是format string,以保证至少两位数。然后我们调用.ToArray()
将其全部汇总到与String.Join()
兼容的结构中。