[英]vb.net access to folder denied
I searched everywhere for this error, changing the manifest file of the program to run as administrator but nothing changed, im making a program for myself to get video streaming links where i put the first part of it in the textbox1 and the second part on the textbox2 these get put together and a number for the episode gets added but when i try to save a txt file with all the links i cant save it because the access is 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
I Managed to make it work but if i want to use it again without closing the form the button doesnt do anything. 我设法使其工作,但如果我想再次使用它而不关闭表格,则该按钮不会执行任何操作。
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
The big thing I saw was use a SaveFileDialog rather than a FolderBrowserDialog . 我看到的最大的事情是使用SaveFileDialog而不是FolderBrowserDialog 。 But there's a lot more you can clean up, too:
但是,您还可以进行更多清理:
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
The meat of the method is this code: 该方法的主要内容是以下代码:
Enumerable.Range(1, ep).
Select(Function(e) String.Format("{0}{1:00}{2}{3}", TextBox1.Text, e, TextBox2.Text, vbCrLf)).
ToArray()
It uses the Enumerable.Range()
function to generate a sequence of integers from 1 to the number of episodes parsed earlier into the ep
variable from TextBox3
. 它使用
Enumerable.Range()
函数生成一个整数序列,该整数序列的范围是1到之前从TextBox3
解析为ep
变量的情节数。 Then it uses the IEnumerable<T>.Select()
function to create a projection of those numbers to the desired strings. 然后,它使用
IEnumerable<T>.Select()
函数创建这些数字到所需字符串的投影。 Select()
accepts a delegate argument, which was supplied here as lambda expression . Select()
接受委托参数,该参数在这里作为lambda expression提供 。 This lambda expression uses String.Format()
to put each string together. 此lambda表达式使用
String.Format()
将每个字符串放在一起。 Specifically, the episode number goes in the {1:00}
placeholder, where the :00
portion is a format string to guarantee at least two digits. 具体来说,剧集编号放在
{1:00}
占位符中,其中:00
部分是一个格式字符串,以确保至少两位数。 Then we call .ToArray()
to roll it all up in a structure that will be compatible with String.Join()
. 然后,我们调用
.ToArray()
将其全部汇总到与String.Join()
兼容的结构中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.