I'm writing a C# WinForms app using DirectX.AudioVideoPlayback (DirectX v9.0c). I open an audio file from a binary file, convert it to a Base64 string, string it into a byte array, then writie it to a temp file on the hard disk. I have a peculiar problem when opening a file from a temp file the second time around.
Here's the code:
private void OpenBinaryFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open Binary File...";
ofd.Multiselect = false;
ofd.Filter = "Binary File|*.bin";
if (DialogResult.OK == ofd.ShowDialog())
{
glbstrFilePath = ofd.FileName;
// Read Base64 string from binary file...
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
strFileType = br.ReadString();
String strAudioString = br.ReadString();
fs.Close();
br.Close();
// Convert byte[] to audio...
byte[] AudioInBytes = Convert.FromBase64String(strAudioString);
File.WriteAllBytes(@"C:\MyTemp\thisaudio", AudioInBytes);
audio = Audio.FromFile(@"C:\MyTemp\thisaudio", false);
File.SetAttributes(@"C:\\MyTemp\\thisaudio", FileAttributes.Normal);
File.Delete(@"C:\MyTemp\thisaudio");
}
If I open a file once using the above code, the file opens, converts and loads without a problem. If I try to load a file again using the above code, I get this:
"System.UnauthorizedAccessException was unhandled HResult= -2147024891 Message=Access to the path 'C:\MyTemp\thisaudio' is denied. Source=mscorlib"
So how can I prepare this app to open a file every time without crashing? I've tried setting the file's attributes to 'Normal' and running VS 2015 as Administrator, but neither works. I've googled "Access to path is denied C# WinForms DirectX.AudioVideoPlayback" and none of the search results, including many posted here on StackOverflow, have a solution that works for me. Also, I'll provide the full stack trace upon request.