当我的程序读取行File.Delete("upv/version.txt");
时出现此错误,但我不知道问题出在哪里以及如何解决问题。有人能帮助我吗?
System.IO.IOException:'进程无法访问该文件 ' C:\用户\ XXX \源\回购\ WpfApp1 \ WpfApp1 \ BIN \调试\ UPV \ version.txt' 因为它正被另一个进程使用。'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.ComponentModel;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
btnPlay.IsEnabled = false;
if (VerifyVersion())
btnPlay.IsEnabled = true;
else
Download("http://localhost/","text.rar");
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("Game.exe");
}
private void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
}
private string VersionActuelle;
private bool VerifyVersion()
{
StreamReader VersionReader = new StreamReader("upv/version.txt");
string VersionClient = VersionReader.ReadToEnd();
VersionClient = VersionClient.Replace(System.Environment.NewLine, string.Empty);
VersionActuelle = ReadRemoteTextFile("http://localhost/upv/version.txt");
VersionActuelle = VersionActuelle.Replace(System.Environment.NewLine, string.Empty);
if (VersionActuelle == VersionClient)
return true;
else
return false;
}
private string ReadRemoteTextFile(string Url)
{
Uri uri = new Uri(Url);
WebRequest wRequest = WebRequest.Create(uri);
WebResponse wResponse = wRequest.GetResponse();
Stream ResponseStream = wResponse.GetResponseStream();
StreamReader sReader = new StreamReader(ResponseStream);
string Temp = sReader.ReadToEnd();
return Temp;
}
private void Download(string Url, string DownloadTo)
{
WebClient wClient = new WebClient();
wClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(UpdateProgressChange);
wClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(UpdateDone);
wClient.DownloadFileAsync(new Uri(Url), DownloadTo);
}
private void UpdateProgressChange(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void UpdateDone(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//MessageBox.Show("Mise à jour terminée! Vous pouvez désormais jouer.", "Notification");
btnPlay.IsEnabled = true;
File.Delete("upv/version.txt");
using(System.IO.TextWriter sWriter = File.CreateText("upv/version.txt"))
{
sWriter.Write(VersionActuelle);
}
}
}
}
答案 0 :(得分:0)
当你在VerifyVersion方法中读取文件时,你需要处理文件..你需要放一个VersionReader.Close(); o如果使用Using子句来处置文件,那就更好了。
答案 1 :(得分:0)
每当您使用 StreamReader 或 StreamWriter 时,请调用 Dispose 关闭该特定实例,但您不会发现错误发生试。
在你的情况下,
string VersionClient = VersionReader.ReadToEnd();
VersionReader.Dispose();
并且
StreamReader sReader = new StreamReader(ResponseStream);
string Temp = sReader.ReadToEnd();
sReader.Dispose();
修改:在使用使用时,您不需要关闭StreamWriter实例,因为它会自动刷新