进程无法访问该文件,因为它正在被另一个进程使用
本文关键字:进程 另一个 因为 访问 文件 | 更新日期: 2023-09-27 17:50:47
进程无法访问文件'D:'Projects'DRPE_NET'Sample.FluentNHibernate.UI'TemporaryFile'Param_17_03_2014'Param_17_03_2014.log',因为该文件正在被其他进程使用。
我想可能是IO或其他什么请帮助我
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
string sourcePath = System.IO.Path.Combine(currentPath, "TemporaryFile");
string sourceFile = System.IO.Path.Combine(sourcePath, file_name);
string transfert = System.IO.Path.Combine(currentPath, "TransfertFiles");
string transfertFiles = System.IO.Path.Combine(transfert,"export_parametrage.bat");
if (System.IO.Directory.Exists(sourcePath))
{
// delete files in directory
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string temp in files)
{
System.IO.File.Delete(temp);
}
// delete directories in directory
string[] diry = System.IO.Directory.GetDirectories(sourcePath);
foreach (string temp in diry)
{
// delete files in directory of directory
string sourcePath1 = System.IO.Path.Combine(sourcePath, temp);
string[] files1 = System.IO.Directory.GetFiles(sourcePath1);
foreach (string temp1 in files1)
{
System.IO.File.Delete(temp1);
}
System.IO.Directory.Delete(temp);
}
System.IO.Directory.CreateDirectory(sourceFile);
}
else if (!System.IO.Directory.Exists(sourcePath))
{
System.IO.Directory.CreateDirectory(sourcePath);
System.IO.Directory.CreateDirectory(sourceFile);
}
// le mot de passe
string inipath = System.IO.Path.Combine(currentPath, "ConfigFile.ini");
IniFile ini = new IniFile(inipath);
string m_d_p = ini.IniReadValue("Transfert", "motDePasse");
string s_i_d = ini.IniReadValue("Transfert", "sid");
string user = ini.IniReadValue("Transfert", "user");
string parametre = user + "/" + m_d_p + "@" + s_i_d;
Process p = new Process();
p.StartInfo.FileName = transfertFiles;
p.StartInfo.Arguments = "" + sourceFile + " " + file_name + " " + parametre;
p.Start();
I: if (System.IO.Directory.Exists(sourceFile))
{
string[] files = System.IO.Directory.GetFiles(sourceFile);
if (files.Count() == 2)
{
try
{
string t = file_name + ".log";
StreamReader file = new
StreamReader(System.IO.Path.Combine(sourceFile, t));
p.Kill();
file.Close();
goto II;
}
catch
{
goto I;
}
}
else
{
goto I;
}
}
II:
ConfigFile.ini
[Transfert]
motDePasse=rrrr
sid=rrrr
user=rrrr
fromuser=rrrr
public class IniFile
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
/// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
path = INIPath;
}
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, this.path);
return temp.ToString();
}
}
替换此语句:
StreamReader file = new StreamReader(System.IO.Path.Combine(sourceFile, t));
p.Kill();
file.Close();
与这个:using(StreamReader file = new StreamReader(System.IO.Path.Combine(sourceFile,t)))
{
p.Kill();
}
尝试如下打开文件:
string fileContents;
try
{
using (FileStream fs = new FileStream(Path.Combine(sourceFile, t), FileMode.Open, FileAccess.Read, FileShare.Read))
using (StreamReader reader = new StreamReader(fs))
{
fileContents = reader.ReadToEnd();
}
}
catch (IOException)
{
p.Kill();
p.WaitForExit(1000);
goto I;
}
请注意,Kill
方法是异步执行的,所以您需要等待。