在 C# Visual Express 2010 中拒绝访问路径

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

我正在使用 Visual C# Express 10。我正在尝试从另一个 .avi 文件创建、打开和写入一个 .avi 文件。我正在为你的项目使用 Aforge dll。单击按钮编写了以下代码。

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);

我在上一行中遇到错误

            // 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();

我收到的错误是 "Access to the path 'C:\test.avi' is denied"。 我在 Whosebug 中尝试了一些解决方案,其中包括更改 C 驱动器的权限和使用环境文件夹。请就此提出建议。

编辑: 抱歉耽搁了。我也不能写日志文件。我正在尝试上传图片

编辑 2: 这是例外。我从文本框中得到它,然后把它打出来。不过我用 xxxx 替换了我的名字。

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

根据您使用 c# 的经验,我的猜测是您 test.avi 打开了

更好的做法是将 for 循环包装在 try/catch 中,如下所示:

for (int i = 0; i < 100; i++)
{
   try
   {

       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();
   }
   catch (Exception ex)
   {
   //show my exception
   }
}

我还建议您也查看 Using Statements 来处理资源

好的。我通过这样做解决了我的问题。 我将保存文件路径更改为 c:users..desktop 显然,我们不能直接在 C: 中写入。 如果在 C:(如 C:\test\test.avi)中创建新文件夹,它也适用 这就是解决方案。 而且,我尝试了 System.IO.File.Create(fileName)。也可以。 祝大家好运。