Search code examples
c#c#-4.0filestreamaforge

Access to the path is denied in C# Visual Express 2010


I'm using Visual C# Express 10. I'm trying to create, open and write to a .avi file from another .avi file. I'm using Aforge dlls for y project. The following code was programmed in a button click.

OpenFileDialog toy = new OpenFileDialog();

            toy.Filter = "AVI files (*.avi)|*.avi|All files (*.*)|*.*";
            toy.Title = "Open an AVI file";
            toy.ShowDialog();
            string wish = toy.FileName;
            VideoFileReader reader = new VideoFileReader();
            VideoFileWriter writer = new VideoFileWriter();
            string fileName = @"C:\test.avi";
            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);

I'm getting error in above line

            // open video file
            reader.Open(wish);
            for (int i = 0; i < 100; i++)
            {
                Bitmap videoFrame = reader.ReadVideoFrame();
                int w = videoFrame.Width;
                int h = videoFrame.Height;
                writer.Open(@"C:\test.avi",w, h);
                videoFrame.Save(i + ".bmp");
                // dispose the frame when it is no longer required
                //videoFrame.Dispose();
            }
            reader.Close();

The error I receive is "Access to the path 'C:\test.avi' is denied". I have tried a few solutions in StackOverflow which included changing permissions of C drive and using environmental folders. Please advise me on this.

EDIT: Sorry for the delay. I cant write the log file as well. I'm trying to upload a pic

EDIT 2: This is the exception. I got it from textbox and just typed it out. I replaced my name with xxxx though.

System.UnauthorizedAccessException: Access to the path 'C:\test.avi' is denied.
at System.IO._Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 buffersize, FileOptions)
options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy,
Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at VideoFramesTest.Form1.OpenAndSave() in
C:\Users\XXXX\documents\visual studio
2010\projects\VideoFramestest\VideoFramestest\Form1.cs:line42

Solution

  • Ok. I solved my problem by doing this. I changed the save file path to c:users....desktop Apparently, we cant write directly in C:. It also works if a new folder is created in C: (like C:\test\test.avi) Thats the solution. And also, I tried System.IO.File.Create(fileName). Works as well. Good luck everyone.