C# UnauthorizedAccessException

EDIT: FIXED: I’ve waited 2 days to post this and of course, the moment after I post it I fix it. Apparently FileInfo.Directory does not return the file’s directory, but the directory it’s in. So I had to add it’s name to it’s directory and everything was ok.

Hey all! First post in this section :open_mouth:

“UnauthorizedAccessException was unhandled.
Access to the path ‘Z:\music’ is denied.”

So here’s the deal. I’m writing my own basic audioPlayer because I don’t need all the extra function that winamp, mediaPlayer or even xmPlay has. It’s going along nicely, but as alway my code slowly becomes a huge mess which is why I decided to rewrite the whole thing. So now I have a nice and solid, clear managed solution.

The only problem now is that an exception is thrown when I try to read the ID3 tags of a file. The code itself has not changed, just it’s position! There is no reason at all why this suddenly wouldn’t work so I think this is very strange. Here’s the code:

To add every music file from a certain folder you need to first select that folder:


private void addFoldersButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Pick your folder";
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                mainForm.PlaylistManager.CreatePlaylistFromFolder(folderDialog.SelectedPath.ToString());
            }                
        }       

Method: CreatePlaylistFromFolder:


public List<Song> CreatePlaylistFromFolder(string folderDirectory)
        {
            List<Song> list = new List<Song>();
            DirectoryInfo directoryInfo = new DirectoryInfo(folderDirectory);

            List<FileInfo> fileInfoList = getFilesInDirectory(directoryInfo, new List<FileInfo>());
            if (fileInfoList.Count > 0)
            {
                list = createSongListFromFileList(fileInfoList); 
            }
            return list;
        }

Method: getFilesInDirectory:


private static List<FileInfo> getFilesInDirectory(DirectoryInfo directory, List<FileInfo> fileInfoList)
        {
            DirectoryInfo[] directoriesOfDirectory = directory.GetDirectories();
            FileInfo[] filesOfDirectory = directory.GetFiles();
            foreach (FileInfo fileInfo in filesOfDirectory)
            {
                if (fileInfo.Extension == ".mp3" || fileInfo.Extension == ".wav")
                {
                    fileInfoList.Add(fileInfo);
                }
            }
            foreach (DirectoryInfo directoryInfo in directoriesOfDirectory)
            {
                getFilesInDirectory(directoryInfo, fileInfoList);
            }
            return fileInfoList;
        }

Method: createSongListFromFileList:


private static List<Song> createSongListFromFileList(List<FileInfo> fileInfoList)
        {
            List<Song> list = new List<Song>();

            foreach (FileInfo fileInfo in fileInfoList)
            {
                list.Add(SongCreator.CreateNewSong(fileInfo.Directory.ToString()));
            }
            return list;
        }

The last code that actually read the ID3 tags so far (which obviously needs a lot of work still :stuck_out_tongue: ). The FileStream is where it all goes wrong!


public static Song CreateNewSong(string fileDirectory)
        {
            string theTitle = "unassigned";
            string theArtist = "unassigned";
            string theAlbum = "unassigned";
            string theYear = "unassigned";
            string theComment = "unassigned";
            string theGenre = "unassigned";

            //FileStream file = new FileStream(fileDirectory, FileMode.Open);
            FileStream file = new FileStream(fileDirectory, FileMode.Open, FileAccess.Read);
            try
            {
                Byte[] theByteBuff = new Byte[128];
                file.Seek(-128, SeekOrigin.End);
                file.Read(theByteBuff, 0, 128);
                System.Text.ASCIIEncoding theEncoding = new System.Text.ASCIIEncoding();
                string theTagInfo = theEncoding.GetString(theByteBuff);
                if (theTagInfo.Contains("TAG"))
                {
                    theTitle = removeControlCharactersFromString(theTagInfo.Substring(3, 30).Trim());
                    theArtist = removeControlCharactersFromString(theTagInfo.Substring(33, 30).Trim());
                    theAlbum = removeControlCharactersFromString(theTagInfo.Substring(63, 30).Trim());
                    theYear = removeControlCharactersFromString(theTagInfo.Substring(93, 4).Trim());
                    theComment = removeControlCharactersFromString(theTagInfo.Substring(97, 30).Trim());
                    theGenre = removeControlCharactersFromString(theTagInfo.Substring(127, 1).Trim());
                }
            }
            catch 
            {
                System.Windows.Forms.MessageBox.Show("Can't decode id3 tag of file: " + fileDirectory);                
            }
            file.Close();
            return new Song(theTitle, theArtist, theAlbum, theYear, theComment, theGenre, fileDirectory); 
        }

The strangest thing is that this stuff actually works. The CreateSong method was simply re-located and was converted to a static method for example, so there’s no reason that this shouldn’t work. Also when I test the CreateSong method by inputting a string directory (manually) it works! So WTF is going on here? I hope you guys can help me.

Thanks