-1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
manicdrummer
  • 161
  • 3
  • 14
  • Did you try to run Visual Studio as Administrator? – m1o2 Apr 10 '16 at 23:15
  • 2
    Did you dispose of the read stream? – BanksySan Apr 10 '16 at 23:22
  • Close your binary reader before your file stream – jegtugado Apr 10 '16 at 23:58
  • thanks for your quick responses, guys. Yes, I tried running VS as Administrator, disposed of the read stream and closed the binary before file stream. Still no success. The code I posted above runs great the first time. If I run it a second time, that's where I get the error message. Maybe I should do some cleanup somewhere before running the code a second time? – manicdrummer Apr 11 '16 at 00:05
  • Its not clear exactly which line is throwing your exception. Is it happening during your last line, call to File.Delete? I would suggest you to look into the stack trace and see which line is failing. Also, your file name looks like a directory name. Try giving an extension like "thisaudio.bin" – loopedcode Apr 11 '16 at 00:08
  • what if you include the file extension of the "thisaudio" – JC Borlagdan Apr 11 '16 at 00:59

1 Answers1

-1

According to https://msdn.microsoft.com/en-us/library/windows/desktop/bb324224(v=vs.85).aspx the Audio class is disposable so you need to use using

using (var audio = Audio.FromFile(@"C:\MyTemp\thisaudio", false))
{
}
File.SetAttributes(@"C:\MyTemp\thisaudio", FileAttributes.Normal);
File.Delete(@"C:\MyTemp\thisaudio"); //edit: code correction
Leaves
  • 35
  • 1
  • 9
Sergei Zinovyev
  • 1,238
  • 14
  • 14