Reputation: 61
The process cannot access the file 'D:\Projects\DRPE_NET\Sample.FluentNHibernate.UI\TemporaryFile\Param_17_03_2014\Param_17_03_2014.log' because it is being used by another process.
i think probably io or anything else please help me
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();
}
}
Upvotes: 1
Views: 748
Reputation: 14059
Try to open a file as follows:
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;
}
Please note, that Kill
method executes asynchronously, so you need to wait.
Upvotes: 0
Reputation: 26209
Replace This:
StreamReader file = new StreamReader(System.IO.Path.Combine(sourceFile, t));
p.Kill();
file.Close();
WIth This:
using(StreamReader file = new StreamReader(System.IO.Path.Combine(sourceFile,t)))
{
p.Kill();
}
Upvotes: 2