简体   繁体   English

该进程无法访问该文件,因为该文件正在被另一个进程使用

[英]The process cannot access the file because it is being used by another process2

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. 该进程无法访问文件'D:\\ Projects \\ DRPE_NET \\ Sample.FluentNHibernate.UI \\ TemporaryFile \\ Param_17_03_2014 \\ Param_17_03_2014.log',因为该文件正在被另一个进程使用。

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();

    }
  }

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();
}

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. 请注意, Kill方法是异步执行的,因此您需要等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该进程无法访问文件.exe,因为它正在被另一个进程使用 - The process cannot access the file .exe because it is being used by another process 该进程无法访问&#39;xml&#39;文件,因为它正由另一个进程使用 - The process cannot access the 'xml' file because it is being used by another process 进程无法访问文件 <filepath> 因为它正在被另一个进程使用 - The process cannot access the file <filepath> because it is being used by another process 该进程无法访问该文件,因为该文件正由另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process “进程无法访问该文件,因为该文件正在被另一个进程使用” - “Process cannot access the file because it is being used by another process” 该进程无法访问该文件,因为它正由另一个进程使用 - The process cannot access the file, because it is being used by another process 进程无法访问图像文件,因为它正在被另一个进程使用 - process cannot access the image file because it is being used by another process 错误:进程无法访问文件“...”,因为它正由另一个进程使用 - Error: The process cannot access the file '…' because it is being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM