How to avoid excluding unauthorized access?
Below is the code.
//string[] directories;
List<string> dirs = new List<string>(Directory.EnumerateDirectories("C:\\Users\\Josh"));
//string[] Files;
//directories = Directory.GetDirectories(@"C:\Users\Josh\", "*", SearchOption.AllDirectories);
SqlConnection myConn = new SqlConnection("Server=Josh-PC;database=Music;User ID=Josh; Password=Climber94; Trusted_Connection=True;");
string removeText = "Delete from Music.dbo.SongNamesAndInfo";
SqlCommand RemoveEntry = new SqlCommand(removeText, myConn);
myConn.Open();
RemoveEntry.ExecuteNonQuery();
myConn.Close();
//for (int d = 0; d < directories.Length; d++)
foreach (var dir in dirs)
{
List<string> files = new List<string>(Directory.EnumerateFiles(dir));
foreach (var file in files)
{
FileInfo oFileInfo = new FileInfo(file);
myConn.Open();
string cmdText = "Insert INTO Music.dbo.SongNamesAndInfo " +
"(Name,dtCreationTime,Extension,Length,DirectoryName)" +
"VALUES(@Name,@dtCreationtime,@Extension,@Length,@DirectoryName)";
SqlCommand addCmd = new SqlCommand(cmdText, myConn);
DateTime dtCreationTime = oFileInfo.CreationTime;
addCmd.Parameters.AddWithValue("@Name", oFileInfo.Name);
addCmd.Parameters.AddWithValue("@dtCreationtime", dtCreationTime);
addCmd.Parameters.AddWithValue("@Extension", oFileInfo.Extension);
addCmd.Parameters.AddWithValue("@Length", oFileInfo.Length.ToString());
addCmd.Parameters.AddWithValue("@DirectoryName", oFileInfo.DirectoryName);
if (oFileInfo.Extension.ToLower() == ".mp3" || oFileInfo.Extension.ToLower() == ".avi" ||
oFileInfo.Extension.ToLower() == ".mkv" || oFileInfo.Extension.ToLower() == ".m4a" ||
oFileInfo.Extension.ToLower() == ".aac" || oFileInfo.Extension.ToLower() == ".wav" ||
oFileInfo.Extension.ToLower() == ".mpa" || oFileInfo.Extension.ToLower() == ".wma" ||
oFileInfo.Extension.ToLower() == ".flv" || oFileInfo.Extension.ToLower() == ".m4v" ||
oFileInfo.Extension.ToLower() == ".mpg" || oFileInfo.Extension.ToLower() == ".mov" ||
oFileInfo.Extension.ToLower() == ".wmv" || oFileInfo.Extension.ToLower() == ".mp4")
{
addCmd.ExecuteNonQuery();
}
myConn.Close();
}
so what I do is take a list of music and movies and put properties there in sql database, but I want it to search the whole directory in the user file. due to appdata im getting
The System.UnauthorizedAccessException was unhandled.
how can i avoid this? the mistake is where it pulls all files from Josh's user folder.
source to share
Some files are system files (like the ones in the user folder, like in your case, like AppData, etc.). you can either ask for permission or ignore them
FileIOPermission f = new FileIOPermission(PermissionState.Unrestricted);
f.AllLocalFiles = FileIOPermissionAccess.Read;
try
{
f.Demand();
//your code for processing files
}
catch (SecurityException s)
{
//cannot get permissions for files.got exception
Console.WriteLine(s.Message);
}
I suggest you take a look at code access security https://msdn.microsoft.com/en-us/library/930b76w0(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/0d005ted(v=vs.90).aspx
source to share
Use Directory.EnumerateDirectories to access each folder one at a time and ignore or log those that throw exceptions. This way you get as much as possible.
If you want to know how to access a directory, it depends on the permissions of your account.
source to share
You have to use enum Environment.SpecialFolder .
use Environment.GetFolderPath(Environment.SpecialFolder.MyMusic )
for music folder or Environment.GetFolderPath(Environment.SpecialFolder.UserProfile ) + @"\Downloads";
etc. specially. This is because, ideally, you won't be able to access all of the folders and subfolders of Windows Explorer itself in the custom folders. This is where it throws the exception.
so your code will be something like this
string[] directories;
string[] Files;
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic ) ;
directories = Directory.GetDirectories(folder);
source to share