将VB项目设置为受信任的访问 [英] Set VB project to trusted access
问题描述
我正在尝试执行获取文件方法,但由于某种原因,代码错误或程序特权未满.我认为代码是正确的,但Catch ex As UnauthorizedAccessException我认为可能会阻止扫描进入子文件夹,这是我此时扫描应用程序的主要目标.
我需要用户选择一个根目录,例如C:\,然后使用使用get file方法在给定的根目录中搜索开始扫描按钮.
起始扫描代码为:
I am trying to perform a get file method but for some reason either the code is wrong or the program privlidges are not full. I figure the code to be correct but the Catch ex As UnauthorizedAccessException I think may be keeping the scan from going into the subfolders which is my main goal at this point for my scanning application.
I need the user to choose a root directory such as C:\ then the start scan button with use the get file method to search within the given root directory.
the start scan code is:
Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
Try
Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
For Each foundFile As FileInfo In files
If fileList.Contains(foundFile.Name.ToUpper) Then
ListBox3.Text &= Environment.NewLine & foundFile.FullName
My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
End If
Next
Catch ex As UnauthorizedAccessException
Debug.WriteLine(String.Format("Could not access directory '{0}'.", myDir.FullName))
End Try
请告诉我这段代码是否有什么问题,如果不是,我真的需要知道如何设置该程序以完全访问根目录中的所有文件夹和子文件夹,这要感谢您这么做..
Please tell me if anything is wrong with this code and if not I truly need to know how to set the program to have full access to all folders and subfolders in the root directory thanks for taking a stab at this..
推荐答案
程序中的另一层?我不知道...
此类似乎使您应该事先知道您拥有哪些文件的权限,而哪些人没有它们的权限才能使用它.
如果我正确理解了您的代码,则需要递归搜索一些路径.嗯...
你不能做这样的事情(我为C#感到抱歉,但我真的很讨厌VB中像BEGIN ... EN这样的语法的语言)?
公共无效搜索(字符串路径)
{
字符串[]文件=新字符串[0],
dirs =新字符串[0];
试试
{
files = Directory.GetFiles(path,"*.*",SearchOption.TopDirectoryOnly);
dirs = Directory.GetDirectories(path);
}
catch(UnautorizedAccessExxeption e)
{
//用Exception做一些事情
}
foreach(文件中的var文件)
{
//列出找到的文件或对它们进行处理
}
foreach(目录中的var目录)
{
//深入树
search(dir);
}
}
当您尝试访问无法但不止于此的文件而只是转到树中的另一个早午餐时,此代码将处理异常.
希望对您有帮助!
#Edited
尝试用代码行交换我在此处发布的功能
Another layer in program? I don''t know...
This class seems like you should know in advance to which files you have permission and to which you does not in order to use it.
If i understood your code correctly you want to search recursively some path. Hm...
Can''t you do something like this (I''m sorry for C# but i really hate languages with syntax BEGIN ... EN like in VB)?
public void search(string path)
{
string[] files = new string[0],
dirs = new string[0];
try
{
files = Directory.GetFiles(path,"*.*",SearchOption.TopDirectoryOnly);
dirs = Directory.GetDirectories(path);
}
catch(UnautorizedAccessExxeption e)
{
//do something with Exception
}
foreach(var file in files)
{
//list finded files or do something with them
}
foreach(var dir in dirs)
{
//go deeper in tree
search(dir);
}
}
This code will handle exception when you will try access files that you cant but not stop there and just go to another brunch in tree.
I hope it will help you!
#Edited
Try swapping a function i posted here with line of code
Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
然后将所有与您的参数匹配的文件汇总.
它应该可以正常工作:)
and in that fuction aggregate all files matching your parameters.
It should work fine :)
文本框?这是Win程序还是WebPage?
我对VB不够熟悉,无法从这段特定的代码中分辨出这一点,但是如果您尝试在IIS中这样做:您不能,您不应该,甚至不应该尝试这样做.这会造成巨大的安全风险.
当然,您仍然可以强制执行此操作,但是为什么呢?
我想您不想让一些匿名用户删除root目录中的所有文件,对吗?
A TextBox? Is this a Win program or a WebPage?
I am not familiar enough with VB to tell this from this particular chunk of code, but if you trying do this in IIS: you can''t, you shouldn''t and you shouldn''t even try to do that. It would cause a huge security risk.
Of course you can force it anyway, but why?
I suppose you don''t want some anonymous user delete all files in root dir, do you?
这篇关于将VB项目设置为受信任的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!