Copy and paste with C# - c#

I know this is asked many times before but it s not what i look for, to make copy paste in c# we use;
//Copy
Clipboard.SetDataObject("String to copy");
//Paste
IDataObject iData = Clipboard.GetDataObject();
I only want to use copy, after using:
Clipboard.SetDataObject("String to copy");
when i close the program, then when i right click and paste on a txt file nothing happens!
So Clipboard class seems doesnt help, so i need another solution.

Clipboard.SetDataObject("String to copy", true);
That boolean value at the end specifies whether the string should remain in the clipboard after the application closes, and is set to false as default.

You want to use second overload of SetDataObject(object, bool).
When bool is set to true, data will remain after application is closed.

Did you try to use Clipboard.SetText(your_String)?
This works for me.

Related

C# and WebClient.UploadFileAsync: howto retain modified date? [duplicate]

Is it possible to copy a file or a folder from one location to another without modifying its attribute data? For example if I have a folder on a network drive and it was created on 2/3/2007 and I want to copy it to my c: drive .. but leave the date/time stamp as 2/3/2007...is that possible?
I'm not sure if it is possible; however you can use the methods within System.IO.File and System.IO.Directory to reset these attributes back to what they were originally.
Specifically the SetCreationTime and SetModificationTime methods will be of most value to you in this case.
I did something as shown below:
File.SetCreationTime(tgtFile, File.GetCreationTime(srcFile));
File.SetLastAccessTime(tgtFile, File.GetLastAccessTime(srcFile));
File.SetLastWriteTime(tgtFile, File.GetLastWriteTime(srcFile));
When you copy a file, it will retain the modified date, however the created date will be changed. I doubt there will be an easy way to retain the created date.
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=net-7.0
The attributes of the original file are retained in the copied file.

Microsoft.Office.Interop.Word.Document - are COM calls to this object async or sync?

I am attempting to extract RTF code out of a word instance that my .NET C# application launches. The code that performs this is:
Clipboard.Clear();
//document is of type Microsoft.Office.Interop.Word.Document
document.Application.Activate();
document.Select();
document.Content.Copy();
if (Clipboard.ContainsData(DataFormats.Text) &&
Clipboard.ContainsData(DataFormats.Rtf))
{
DocumentContent = Clipboard.GetText(TextDataFormat.Rtf);
}
Clipboard.Clear();
What is periodically happening is that when the call is made to Clipboard.GetText it is returning String.Empty. So I was wondering if it is possible that the COM call to the Content.Copy has not finished yet. The reason I suspect this is because if I put a break point on DocumnetContent=... and it comes back as String.Empty I can just move the debugger back one line and call the GetText line again and this time it will contain the text. Similarly if it comes back with String.Empty I can go into notepad and hit control+V and it will paste the copied text that I expect to be on the clipboard.
I must stress that this behavior is not consistent. I can't reproduce it at will but if I perform the action enough (usually 1 out of 20 will trigger this behavior) I'll eventually get it.
Thank you for your help and please let me know of any clarifications needed.

Make every Copy and cut paste opearation Paste Special

I am working on C# VSTO(excel). I have created excel workbook project.
I have been trying to implement Paste Special for my workbook.
Manually, we do paste special like This
But i need C# code to do the same.
Is there any way so i can apply it for every copy/cut paste operation in my excel workbook?
I don't want to use VBA Macros as it asks everytime user to whether he wants to ENABLE MACROS OR NOT and hence is there another way o accomplish this.
Have you tried PasteSpecialmethod over ranges, it provides numerous options to copy formats/ column widths/ fomats with values etc.
Also there is another copy special to copy/paste as picture.
to copy Range:
Range.CopyPicture(xlPrinter, xlPicture);
// the range gets copied in clipboard
// there are options available like xlScreen in case you want to copy as it appears on screen.
Sheet.Paste()
// this will paste the shape in the sheet, to paste in some range you could use range.Paste()

C# Clipboard Help

I am copying the contents of an Excel file onto the Clipboard within a program I have written. I can then use that data in memory rather than 'chatting' constantly with Excel.
When I have finished with the data, I cal a cleanup method that calls Clipboard.Clear() first and then closes all Excel sheets/workbooks/apps, etc.
The problem is, even though I clear the Clipboard prior to closing the Excel sheets, I get a pop up window still saying there is substantial amount of data on the clipboard. Anybody know why?
Thanks,
Darren.
Not sure why that happens, but have you tried setting _Application.DisplayAlerts = false; (MSDN) before you close the sheets to see if that prevents the warning message?
You could try setting the Excel CutCopyMode property to cancel the current copy information:
Application.CutCopyMode = false;
Another thought is to set the clipboard to String.Empty so the amount of data copied is small enough that it bypasses the warning popup. This may have to be done from the Excel sheet, not the regular clipboard (i.e., copy a cell from the active sheet in Excel).
Perhaps you could use
Application.CutCopyMode = false;
http://msdn.microsoft.com/en-us/library/ff839532.aspx
Well at the end, try copying an empty string in clipboard and leave it, then Excel may not give any warning. But use Excel API to copy empty string in clipboard.

Modifying clipboard contents on clipboard change

I'm doing this in C#, but, I guess, it isn't language specific problem...
I've got some sample code on how to detect when the contents of the clipboard changes. Now I want to modify the text that just got copied (strip some tags) and replace clipboard text with fixed one.
But if I SetDataObject() when I detect the clipboard contents have changed, that will generate the the message that Clipboard contents have changed again. In theory, i guess, it sounds like an infinite loop (in practice for some reason it's not).
What's the strategy to do this modification once, and sent the message down the clipboard monitoring chain?
P.S. As a test I'm doing following, but what happens is Clipboard.SetDataObject.. line gets called twice. I don't understand why, I'd expect it do this infinitely.
case Win32.Msgs.WM_DRAWCLIPBOARD:
String clipboardText = GetClipboardText();
if (!String.IsNullOrEmpty(clipboardText))
{
Clipboard.SetDataObject("test( " + clipboardText + " )!");
}
Win32.User32.SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
Unfortunately, any time you set the Clipboard's data with Clipboard.SetDataObject, you'll "redraw" the clipboard, since you're changing its contents.
The best way to handle this situation is to make your routine check the clipboard contents, and only set it if you actually change the clipboard text.
In your case, you say you're stripping out some tags. Just check the clipboardText string before and after your routine, and if the string is unchanged, don't set it again.
This way, the first time, it'll strip the tags out, then reset. Then fire again, see there are no changes, and not reset the clipboard data.
To prevent the recursion, you should only actually set the clipboard text if there were tags to strip.
To explain the behavior, I would guess that WM_DRAWCLIPBOARD isn't sent recursively, meaning that if the clipboard is changed during a WM_DRAWCLIPBOARD notification, it won't be sent again.
Actually there is a much better way to avoid infinite loop which works even in cases when you are not modifying clipboard content and consequently you cannot compare if you changed it or not.
When you detect WM_DRAWCLIPBOARD message call GetClipboardOwner which will return handle to the window which modified the handle. Once you have the handle you can get the process which owns the window. If the process is different from your process then process the clipboard, otherwise the clipboard was changed by you so ignore it.

Categories

Resources