简体   繁体   English

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

[英]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. 我正在使用Visual C#Express10。我试图从另一个.avi文件创建,打开并写入.avi文件。 I'm using Aforge dlls for y project. 我为您的项目使用Aforge dll。 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". 我收到的错误是“拒绝访问路径'C:\\ test.avi'”。 I have tried a few solutions in StackOverflow which included changing permissions of C drive and using environmental folders. 我在StackOverflow中尝试了一些解决方案,其中包括更改C驱动器的权限和使用环境文件夹。 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. 编辑2:这是例外。 I got it from textbox and just typed it out. 我是从文本框中输入的,只是将其输入。 I replaced my name with xxxx though. 我用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

My guess given your experience with c# would be you have test.avi open 考虑到您使用C#的经验,我的猜测是您可以打开test.avi

a better thing to do would be to wrap your for loop in a try/catch like so: 更好的方法是将您的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
   }
}

I would also suggest you also look at Using Statements to handle resources 我还建议您也查看使用语句来处理资源

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:. 我将保存文件路径更改为c:users .... desktop显然,我们不能直接在C:中编写。 It also works if a new folder is created in C: (like C:\\test\\test.avi) Thats the solution. 如果在C:中创建一个新文件夹,它也可以工作(例如C:\\ test \\ test.avi)。 And also, I tried System.IO.File.Create(fileName). 而且,我尝试了System.IO.File.Create(fileName)。 Works as well. 也可以。 Good luck everyone. 祝大家好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM