尝试 pause/resume 传出消息队列时出现访问被拒绝错误

Getting an access denied error when trying to pause/resume an outgoing message queue

Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices

Public Class PauseOutMessages
    'Declare everything to be in the scope of all methods.
    Dim mgmt As New MSMQManagement
    Dim outqmgmt As MSMQOutgoingQueueManagement
    Dim q As New MSMQApplication
    Dim outgoingQueues As New ArrayList
    Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next

    End Sub

    Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Pause()
        Next
    End Sub

    Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Resume()
        Next
    End Sub

    Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
        myQueue.Send("Test")
        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next
    End Sub
End Class

这是我正在使用的代码,通过将测试消息发送到一个不存在的路径,它会卡在我希望能够调用 MSMQOutgoingQueueManagement.Pause 或 .Resume 的传出队列中启动和停止所有传出队列。

但是,我一直在 mgmt.Pause() 或 mgmt.Resume() 上收到错误消息,说访问被拒绝。我似乎无法找到一种方法来了解传出队列的属性以便能够调整安全设置。任何帮助将不胜感激!

已解决!

原来我只需要以管理员身份启动 visual studio 然后就可以了。