2017-07-12 6 views
0

私は非常に古いwinformアプリケーションを持っています。ユーザーはネットワークフォルダにローカライズされたexeファイルで直接作業します。コードでネットワークフォルダ内にローカライズされたファイルを強制的に削除する

私の大きな問題新しいバージョンをリリースするたびに問題が発生します。アプリの

DLLは、ユーザーによってロックされていると、私は次のエラーを取得する:

The process cannot access the file because it is being used by another process.

(私は新しいバージョンをリリースするために別のアプリを使用します)。

一部のユーザーがまだアプリケーションで作業している場合でも、ファイルを強制的に削除するスクリプトを作成します。

私はどの言語を書いても構いませんが、PowerShell、C#を試しましたが、答えが見つかりませんでした。あなたの質問にあなたがとにかく使用中の予期しないシャットダウンがアプリを与える展開アセンブリを削除強制する方法を求めているので、...

は最初の前に、exeファイルのプロセスを殺すことで試してみてくださいと仮定すると

+0

Iドンプロセスによって使用されているファイルを強制的に削除できると思っています。 [この記事(http://www.techadvisor.co.uk/how-to/windows/how-delete-file-in-use-windows-rename-locked-corrupt-3605330/)は便利ですか? – SCramphorn

+0

ファイルを削除することはできませんが、Windowsでは現在使用中のファイル(myapp.exeからmyapp-old.exeなど)の名前を変更することができます。次に、アプリケーションがロードされると、新しいファイルが使用されます。 –

+0

実行ファイルをPowerShellのStop-ProcessまたはC#のprocess.Kill()で強制終了しようとしましたか?あなたがすでにこの道を歩いていた場合、私は答えとしていくつかの例を起動します。 –

答えて

0

展開する。例えば、C#で(私はちょうど初期の思考の自由秒を持ってまで、未テスト/最適化されていません):

while(Process.GetProcessesByName("process_name").Length > 0) 
{ 
//keep murderizing process while they are running 
foreach (var process in Process.GetProcessesByName("whatever")) 
{ 
    process.Kill(); 
} 
} 

//Should be free now to delete 
+0

こんにちは、あなたの答えに感謝します。私はこのような考えを考えていましたが、プロセスはユーザーのコンピュータにあります。どのように私はそれらのプロセスにアクセスできますか? – Bat

0

Hello! In the remote hosts you can try to do like this:

How-To-Almost-Everything-In-WMI-via-C-Part-Proce

Or like this (using powershell - add a System.Management.Automation.dll in C#):

using System.Management.Automation; 

var xs = PowerShell.Create().AddScript("(gwmi win32_process -ComputerName Host | ? {$_.name -like 'proceesname*'}).Terminate()").AddCommand("out-string"); 
      Collection<PSObject> results = xs.Invoke(); 

      foreach (var str in results) 
      { 


        Console.WriteLine(str); 
        Console.ReadLine(); 

      } 

Or you can use psexec.exe to connect at the remote pc:

string queryString = 
        "SELECT Name" + 
        " FROM Win32_Process"; 

       ConnectionOptions connOptions = new ConnectionOptions(); 

       connOptions.Username = "[email protected]"; 
       connOptions.Password = "password"; 

       SelectQuery query = new SelectQuery(queryString); 

       ManagementScope scope = new System.Management.ManagementScope(@"\\Hostname\root\CIMV2", connOptions); 

       ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
       ManagementObjectCollection processes = searcher.Get(); 

       foreach (ManagementObject mo in processes) 
       { 

        if (mo["Name"].ToString() == "mmc.exe") 
        { 
         Console.WriteLine(mo["Name"].ToString()); 

         Process cmd = new Process(); 

         cmd.StartInfo.FileName = "processname.exe"; 

         cmd.StartInfo.RedirectStandardInput = true; 
         cmd.StartInfo.UseShellExecute = false; 
         cmd.Start(); 
         cmd.StandardInput.WriteLine("psexec.exe \\\\Hostname cmd /c taskkill /IM " + mo["Name"].ToString() + " /f"); 
         cmd.WaitForExit(); 
         Console.ReadKey(); 
        } 

       } 
関連する問題