![](/img/trans.png)
[英]Why does my command line application error out on remote assembly when ran outside Visual Studio?
[英]Why is my clipboard-manipulating application behaving differently when run outside of Visual Studio?
我有一個控制台應用程序,其“輸出類型”為“ Windows應用程序”(以實現無頭行為,即沒有UI,沒有控制台窗口)。 目的是“清除” Windows剪貼板中的文本,即修剪所有前導和尾隨空白並刪除格式。
調試時它很好用,但是當我從資源管理器中運行它時,它只是清除剪貼板中的內容。
這是我的代碼:
[STAThread]
internal static void Main(string[] args)
{
var currentClipboardText = System.Windows.Clipboard.GetText();
// only if the clipboard has text; leave any other content intact.
if(!string.IsNullOrEmpty(currentClipboardText))
{
currentClipboardText = currentClipboardText.Trim();
var pattern = @"[\t\r\n\v\f\u2028\u2029]"; // match vert. whitesp & tabs
currentClipboardText = Regex.Replace(currentClipboardText, pattern, " ");
System.Windows.Clipboard.SetDataObject(currentClipboardText);
}
}
以MS Word格式格式化的示例輸入,已復制到剪貼板:
在dubugger中運行程序后(無論是在“調試”模式還是在“發布”模式,都沒有區別),以下是從剪貼板粘貼回Word的結果:
如果我做同樣的練習,但是通過在Windows資源管理器中(即\\bin\\Debug
目錄中)雙擊該程序來運行該程序,則剪貼板上沒有文本可粘貼到Word中:
這里有什么區別? 為什么在Visual Studio之外無法正常工作?
默認情況下,當應用程序退出時,帶有SetDataObject的系統剪貼板上放置的數據會自動從剪貼板中清除。
使用System.Windows.Clipboard.SetDataObject(currentClipboardText, true);
而是在應用程序退出后將數據保留在剪貼板中。
我已經測試了您的代碼,並且在調試時不起作用。 我改變了這一行:
System.Windows.Clipboard.SetDataObject(currentClipboardText);
附:
System.Windows.Clipboard.SetText(currentClipboardText);
它在調試時和不在調試時都有效。 我認為發生的事情是Word不再將DataObject識別為可粘貼的文本。
我希望這能解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.