0

指定したワークステーションから対象のファイルをアプリケーションで削除したいと考えていました。ソフトウェアを実行するユーザーはターゲット マシンの管理者になるため、テストには次のコードを使用しました。

string strTarget = @"\\" + textBox1.Text + @"\C$\Temp\temp.txt";

            try
            {
                File.Delete(strTarget);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failure to delete: " + ex.Message);
            }

次に、自分のワークステーションと別のテスト マシンの両方に \Temp\Temp.txt ファイルを作成しました。私は両方のマシンの管理者であり、問​​題の UNC パスを介してファイルに手動でアクセスして削除できます。コード デバッガーを実行すると、例外はスローされませんが、ファイルは削除されません。これが失敗するために何が起こっていないのか理解できません。

確認できることや追加する必要のあるコードはありますか? 他の質問を検索しましたが、まだ答えを見つけることができませんでした。

4

1 に答える 1

1

Vista/7 で実行している場合、プログラムは昇格されていない権限で実行されている可能性があります。プログラムを管理者として明示的に実行するか、プロジェクトのマニフェスト ファイルで指定してください。

  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <!--
      UAC Manifest Options
      If you want to change the Windows User Account Control level replace the 
      requestedExecutionLevel node with one of the following.

    <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
    <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
    <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

     If you want to utilize File and Registry Virtualization for backward 
     compatibility then delete the requestedExecutionLevel node.
-->
    <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
  </requestedPrivileges>
于 2012-04-30T23:17:39.037 に答える