[英]Error: 'The process cannot access the file because it is being used by another process' in Visual Studio c#
我在 Visual Studio 中遇到了這個錯誤:
mscorlib.dll 中發生類型為“System.IO.IOException”的未處理異常
附加信息:該進程無法訪問文件“C:\\Users\\aheij\\Desktop\\KinectOutput\\swipe.txt”,因為它正被另一個進程使用。
我嘗試過的:我嘗試使用從與我的類似的其他已解決的 StackOverflow 問題中獲得的這些代碼來嘗試解決問題 - 但即使這樣似乎也不起作用? 我試過檢查文件是否正在使用,但沒有成功。
我也以管理員身份運行 Visual Studio。
該文件不是只讀的。
該文件夾未打開,並且該文件在執行代碼時未在任何其他程序中使用 - 至少不是我可以看到/知道的。
代碼:因此,要向我的代碼添加一些上下文:我正在向免費提供的 Kinect c# BodyBasics SDK v2 代碼中編寫一些快速手勢檢測代碼。 這是我添加的一個輔助方法,當一個人在視野中時會被調用。 如果該人正在執行手勢,則該方法會將幀時間和手勢名稱寫入文本文件。
有一半時間,當代碼正常運行時,手勢識別運行良好。 但是,另一半時間,代碼會中斷/停止,因為寫入文件位會導致錯誤。
下面是我的代碼,用於查看此人是否站在中立位置 - 有點胡說八道,因此對此深表歉意。 我已經評論了錯誤所在的“錯誤”(不出所料):
private void Neutral_stance(VisualStyleElement.Tab.Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
{
CameraSpacePoint left_hand = joints[JointType.HandLeft].Position;
CameraSpacePoint left_elbow = joints[JointType.ElbowLeft].Position;
CameraSpacePoint left_shoulder = joints[JointType.ShoulderLeft].Position;
CameraSpacePoint left_hip = joints[JointType.HipLeft].Position;
CameraSpacePoint right_hand = joints[JointType.HandRight].Position;
CameraSpacePoint right_elbow = joints[JointType.ElbowRight].Position;
CameraSpacePoint right_shoulder = joints[JointType.ShoulderRight].Position;
CameraSpacePoint right_hip = joints[JointType.HipRight].Position;
double vertical_error = 0.15;
double shoulderhand_xrange_l = Math.Abs(left_hand.X - left_shoulder.X);
double shoulderhand_xrange_r = Math.Abs(right_hand.X - right_shoulder.X);
if (bf != null)
{
TimeSpan frametime = bf.RelativeTime;
string path_p = @"C:\Users\aheij\Desktop\KinectOutput\Punch.txt"; //write to punch file
string path_s = @"C:\Users\aheij\Desktop\KinectOutput\swipe.txt"; //write to swipe file
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";
File.AppendAllText(path_p, frametime.ToString() + " Neutral" + Environment.NewLine); //ERROR
File.AppendAllText(path_s, frametime.ToString() + " Neutral" + Environment.NewLine); //ERROR
}
}
}
}
else
{
Gesture_being_done.Text = " Unknown";
File.AppendAllText(path_p, frametime.ToString() + " Unknown" + Environment.NewLine); //ERROR
File.AppendAllText(path_s, frametime.ToString() + " Unknown" + Environment.NewLine); //ERROR
}
}
}
任何將我指向正確軌道的解決方案/想法/建議將不勝感激。 我認為使用“使用流編寫器”方法而不是我在這里使用的方法會很好 - 但我不確定如何? 任何幫助,將不勝感激。
附加信息:使用 Visual Studio 2015; 使用 Windows 10。
旁注:我在某處讀到 Windows 10 中的 Windows 搜索工具會干擾並導致此類問題,所以我需要禁用它?
按照我的建議,我使用了 Filestream 方法並確保文件在使用后關閉。 但是,即使這樣仍然會導致同樣的錯誤。
因此,我也擺脫了快速連續的兩個文件寫入操作。 我不知道這在技術上是否正確或什至是真的,但基於這里的這篇文章: 鏈接,我的錯誤可能會出現,因為我正在嘗試執行第二個“寫入文本文件”行,而前一個“寫入文本”行file' 行仍在執行/寫入同一文件夾和位置 - 因此發生沖突? 請有人糾正我,如果我錯了。
無論哪種方式,這似乎都奏效了。 請參閱下面的我編輯/更正的方法:
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();
}
}
}
請讓我知道是否有任何方法可以使這個更優雅或其他任何我應該注意的答案。 該代碼基於此處找到的代碼: FileStream 教程網站
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.