System.IO.IOException:'该进程无法访问文件'File',因为它正在被另一个进程使用。'

时间:2019-07-23 07:36:09

标签: c# streamreader streamwriter writefile

我正在尝试检查程序的版本,如果不正确,则将其下载并将该版本写入文件。它说它正在被另一个进程使用(我已经检查过了,没有打开的进程可以使用它)

我尝试设置文件属性 我尝试使用该程序集替换文件,但没有用

File.SetAttributes(Environment.CurrentDirectory + "\\bin\\ver\\EXE_Version.txt", FileAttributes.Normal);
File.SetAttributes(Environment.CurrentDirectory + "\\bin\\ver\\DLL_Version.txt", FileAttributes.Normal);

StreamReader EXE_READER = new StreamReader(Environment.CurrentDirectory + "\\bin\\ver\\EXE_Version.txt");
StreamReader DLL_READER = new StreamReader(Environment.CurrentDirectory + "\\bin\\ver\\DLL_Version.txt");

StreamWriter EXE_WRITER = new StreamWriter(Environment.CurrentDirectory + "\\bin\\ver\\EXE_Version.txt", true);
StreamWriter DLL_WRITER = new StreamWriter(Environment.CurrentDirectory + "\\bin\\ver\\DLL_Version.txt", true);

EXE_VERSION = EXE_READER.ReadToEnd();
DLL_VERSION = DLL_READER.ReadToEnd();

string EXE_VERSION_READ = string.Empty;
string DLL_VERSION_READ = string.Empty;

HttpWebRequest EXE_REQUEST = (HttpWebRequest)WebRequest.Create("https://link/exe_version.txt");
using (HttpWebResponse EXE_RESPONSE = (HttpWebResponse)EXE_REQUEST.GetResponse())
{
    using (Stream EXE_STREAM_URL = EXE_RESPONSE.GetResponseStream())
    using (StreamReader EXE_READER_URL = new StreamReader(EXE_STREAM_URL))
    {
        EXE_VERSION_READ = EXE_READER_URL.ReadToEnd();
    }
}

HttpWebRequest DLL_REQUEST = (HttpWebRequest)WebRequest.Create("https://link/dll_version.txt");
using (HttpWebResponse DLL_RESPONSE = (HttpWebResponse)DLL_REQUEST.GetResponse())
{
    using (Stream DLL_STREAM_URL = DLL_RESPONSE.GetResponseStream())
    using (StreamReader DLL_READER_URL = new StreamReader(DLL_STREAM_URL))
    {
        DLL_VERSION_READ = DLL_READER_URL.ReadToEnd();
    }
}

if (EXE_VERSION_READ != EXE_VERSION)
{
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("Found an update, downloading...");
    client.DownloadFile("https://link/EXE_Latest.html", "Zuros.exe");
    EXE_WRITER.WriteLine(EXE_VERSION_READ);
    EXE_WRITER.Close();
    Console.WriteLine("Downloaded EXE version: " + EXE_VERSION_READ);
    Console.ForegroundColor = ConsoleColor.Red;
}
else
{
    Console.WriteLine("No Executable updates were found, checking for DLL updates.");
}

我想将版本文件中的版本从网站上存储的版本更改为版本,以免每次启动时都没有更新。

0 个答案:

没有答案