Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:';该进程无法访问该文件,因为另一进程正在使用该文件;在visualstudioc中#_C#_Visual Studio_File_Runtime Error - Fatal编程技术网

C# 错误:';该进程无法访问该文件,因为另一进程正在使用该文件;在visualstudioc中#

C# 错误:';该进程无法访问该文件,因为另一进程正在使用该文件;在visualstudioc中#,c#,visual-studio,file,runtime-error,C#,Visual Studio,File,Runtime Error,我在Visual Studio中遇到了以下错误: mscorlib.dll中发生类型为“System.IO.IOException”的未处理异常 其他信息:进程无法访问文件“C:\Users\aheij\Desktop\KinectOutput\swipe.txt”,因为其他进程正在使用该文件 我尝试过的: 我曾尝试使用从其他已解决的StackOverflow问题(类似于我的问题)中获得的代码来解决这个问题,但即使这样似乎也不起作用? 我试过了,但没有成功 我还以管理员身份运行VisualStu

我在Visual Studio中遇到了以下错误:

mscorlib.dll中发生类型为“System.IO.IOException”的未处理异常

其他信息:进程无法访问文件“C:\Users\aheij\Desktop\KinectOutput\swipe.txt”,因为其他进程正在使用该文件

我尝试过的: 我曾尝试使用从其他已解决的StackOverflow问题(类似于我的问题)中获得的代码来解决这个问题,但即使这样似乎也不起作用? 我试过了,但没有成功

我还以管理员身份运行VisualStudio

该文件不是只读的

该文件夹未打开,并且在执行代码时,该文件未在任何其他程序中使用-至少我看不到/不知道

代码: 因此,为我的代码添加一些上下文:我正在为免费提供的Kinect c#BodyBasics SDK v2代码编写一些快速手势检测代码。这是我添加的一个helper方法,当一个人在视图中时调用它。如果此人正在执行手势,该方法会将帧时间和手势名称写入文本文件

有一半的时间,当代码工作时,手势识别工作正常。但是,另一半时间,代码会中断/停止,因为写入文件位会导致错误

下面是我的代码,看看这个人是否站在中立的位置-这有点胡扯,所以对此表示歉意。我已经在错误所在的位置对“ERROR”进行了注释(毫不奇怪):

private void Neutral_站姿(VisualStyleElement.Tab.Body主体、iRadonlyDictionary关节、IDictionary关节点、BodyFrame bf)
{
CameraSpacePoint left\u hand=关节[JointType.HandLeft]。位置;
CameraSpacePoint左肘=关节[JointType.ElbowLeft]。位置;
CameraSpacePoint left_肩部=关节[JointType.ShoulderLeft]。位置;
CameraSpacePoint left_hip=关节[JointType.HipLeft]。位置;
CameraSpacePoint right\u hand=关节[JointType.HandRight]。位置;
CameraSpacePoint right_弯头=关节[JointType.ElbowRight]。位置;
CameraSpacePoint right_肩部=关节[JointType.ShoulderRight]。位置;
CameraSpacePoint right\u hip=关节[JointType.HipRight]。位置;
双垂直误差=0.15;
double-shoulderhand\u xrange\u l=Math.Abs(left\u-hand.X-left\u-should.X);
double-shoulderhand\u xrange\u r=Math.Abs(right\u-hand.X-right\u-should.X);
如果(bf!=null)
{
TimeSpan frametime=bf.RelativeTime;
字符串路径\u p=@“C:\Users\aheij\Desktop\KinectOutput\Punch.txt”;//写入Punch文件
字符串路径_s=@“C:\Users\aheij\Desktop\KinectOutput\swipe.txt”;//写入刷卡文件
if(左手Y<左手肘Y)
{
if(右手Y<右肘Y)
{
if(肩手距离<垂直误差)
{
if(肩手距离<垂直误差)
{
手势“正在”完成。Text=“中立”;
File.AppendAllText(path_p,frametime.ToString()+“Neutral”+Environment.NewLine);//错误
File.AppendAllText(path_s,frametime.ToString()+“Neutral”+Environment.NewLine);//错误
}
}
}
}
其他的
{
手势“正在”完成。Text=“未知”;
File.AppendAllText(path_p,frametime.ToString()+“Unknown”+Environment.NewLine);//错误
File.AppendAllText(path_s,frametime.ToString()+“Unknown”+Environment.NewLine);//错误
}
}
}
任何能为我指明正确方向的解决方案/想法/建议都将不胜感激。我认为使用“使用streamwriter”方法与我在这里使用的方法相比会更好,但我不确定如何使用?任何帮助都将不胜感激

附加信息:使用Visual Studio 2015;使用Windows10


旁注:我在某个地方读到,Windows 10中的Windows搜索工具可能会干扰并导致类似的问题,所以我需要禁用它

根据我的建议,我使用了Filestream方法&确保文件在使用后关闭。但是,即使这样,仍然会导致同样的错误

因此,我也摆脱了两个文件写入动作快速连续的情况。我不知道这在技术上是否正确,甚至是正确的,但根据本文:,我的错误可能会出现,因为我正在尝试执行第二行“写入文本文件”,而前一行“写入文本文件”仍在执行/写入同一文件夹和位置-因此发生冲突?如果我错了,请有人纠正我

不管怎样,这似乎已经奏效了。 有关我编辑/更正的方法,请参见下文:

        private void Neutral_stance(Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
    {
        //cameraspace point joint stuff here again (see original post for this bit leading up to the if statements.)

        if (bf != null)
        {
            TimeSpan frametime = bf.RelativeTime;
            string path_s = @"C:\Users\aheij\Desktop\KinectOutput\swipe.txt";

            if (left_hand.Y < left_elbow.Y)
            {
                if (right_hand.Y < right_elbow.Y)
                {
                    if (shoulderhand_xrange_l < vertical_error)
                    {
                        if (shoulderhand_xrange_r < vertical_error)
                        {
                            Gesture_being_done.Text = "  Neutral";

                            FileStream fs_s = new FileStream(path_s, FileMode.Append); //swipe
                            byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Neutral" + Environment.NewLine);
                            fs_s.Write(bdatas, 0, bdatas.Length);
                            fs_s.Close();
                        }
                    }
                }
            }
            else
            {
                Gesture_being_done.Text = "  Unknown";
                FileStream fs_s = new FileStream(path_s, FileMode.Append);
                byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Unknown" + Environment.NewLine);
                fs_s.Write(bdatas, 0, bdatas.Length);
                fs_s.Close();
              }
        }

    }
private void Neutral_站姿(主体、定向索引关节、索引关节点、主体框架bf)
{
//cameraspace point joint stuff再次出现在这里(关于if语句之前的这一位,请参阅原始帖子)
如果(bf!=null)
{
TimeSpan frametime=bf.RelativeTime;
字符串路径_s=@“C:\Users\aheij\Desktop\KinectOutput\swipe.txt”;
if(左手Y<左手肘Y)
{
if(右手Y<右肘Y)
{
if(肩手距离<垂直误差)
{
if(肩手距离<垂直误差)
{
手势“正在”完成。Text=“中立”;
FileStream fs_s=新的FileStream(path_s,FileMode.Append);//滑动
byte[]bdatas=Encoding.Default.GetBytes(frametime.ToString()+“Neutral”+Environment.NewLine);
        private void Neutral_stance(Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
    {
        //cameraspace point joint stuff here again (see original post for this bit leading up to the if statements.)

        if (bf != null)
        {
            TimeSpan frametime = bf.RelativeTime;
            string path_s = @"C:\Users\aheij\Desktop\KinectOutput\swipe.txt";

            if (left_hand.Y < left_elbow.Y)
            {
                if (right_hand.Y < right_elbow.Y)
                {
                    if (shoulderhand_xrange_l < vertical_error)
                    {
                        if (shoulderhand_xrange_r < vertical_error)
                        {
                            Gesture_being_done.Text = "  Neutral";

                            FileStream fs_s = new FileStream(path_s, FileMode.Append); //swipe
                            byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Neutral" + Environment.NewLine);
                            fs_s.Write(bdatas, 0, bdatas.Length);
                            fs_s.Close();
                        }
                    }
                }
            }
            else
            {
                Gesture_being_done.Text = "  Unknown";
                FileStream fs_s = new FileStream(path_s, FileMode.Append);
                byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Unknown" + Environment.NewLine);
                fs_s.Write(bdatas, 0, bdatas.Length);
                fs_s.Close();
              }
        }

    }