|
Hello.
I'm implementing WebDAV COPY method.
public void Copy(string userName, string password, string source, string destination)
{
CredentialCache MyCredentialCache = new CredentialCache();
MyCredentialCache.Add(new System.Uri(source), "Basic", new NetworkCredential(userName, password));
try
{
HttpWebRequest Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(source);
Request.Credentials = MyCredentialCache;
Request.Method = "COPY";
Request.Headers.Add("Destination", destination);
Request.Headers.Add("Overwrite", "F");
HttpWebResponse Response = (System.Net.HttpWebResponse)Request.GetResponse();
Response.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
The method works fine with non-unicode file names.
But, if I want to copy a file with korean letters in its name (http://myDAVserver.myCompany.com/dav/한글.txt) I get the following error:
Message: Specified value has invalid Control characters.
Parameter name: value
I tracked the error, and the exception is being throwed exactly in
Request.Headers.Add("Destination", destination);
Does anybody have an idea how to handle this problem?
Thank you.
|
|
|
|
|
The problem could be a conflict between the character sets allowed in URIs and filenames (on whatever system). I had a quick peek at the RFCs but the allowed character set for URIs didn't leap out at me. (The destination parameter is specified by the WebDAV RFC as being a URI.) Perhaps the documentation of HttpWebRequest specifies what character set it will allow in headers - it should, if it throws an exception when you get it wrong.
Not a solution on a plate, but hopefully this will point you in the right direction.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
HTTP headers have to be 7 bit ASCII, I think, nominally. They are certainly interpreted at best as 8 bit ANSI (Windows western encoding). You will need to encode the Unicode characters.
|
|
|
|
|
Thank you for the answer.
Yes. You are right.
I could have encoded the Unicode characters and URL using HttpUtility.UrlEncode Method.
But the problem is, it is implemented only in .Net 4.0, but unfortunately, I'm writing my program in .Net 3.5 do to the compatibility with "a little bit old OSs" with older versions of .Net.
Still did not find a proper solution.
That was totally wrong.
I tried to use HttpUtility.UrlPathEncode(str) method directly without adding reference to the System.Web Namespace in project's properties:
...
using System.Web;
...
string convertedPath=HttpUtility.UrlPathEncode(path);
...
And after adding reference, I did like this:
...
string convertedPath=System.Web.HttpUtility.UrlPathEncode(path);
...
and it worked.
Thank you guys!
|
|
|
|
|
I am trying to execute a delegate from Method B1 for ButtonC and I get the following error:
Object reference not set to an instance of an object.
Have I setup the delegate correctly, what am I doing wrong.
I appreciate any help.
Thanks in advance,
Michael
namespace Z
{
public delegate void DelC(object sender, EventArgs e);
static partial class A
{
public static void Main()
{
FormC FC = new FormC();
DelC delC = new DelC (FC.buttonC_Click);
}
}
public static class B
{
public static DelC delC = null;
public static void B1()
{
object sender = new object();
EventArgs e = new EventArgs();
delC(sender, e); *****ERROR - Object reference not set to an instance of an object.
}
}
public class FormC
{
public void buttonC_Click(object sender, EventArgs e)
{
SortData();
}
}
}
modified on Tuesday, June 7, 2011 10:11 PM
|
|
|
|
|
In your static class B , delC is set to null and never set to an instance of B.DelC as the error message indicates.
Sort of OT: Why all the static classes? It seems you may be learning and confused by instances and trying to work around it by using static. Bad idea IMO!
If this is just a test code sample then fine, but otherwise; the System.EventHandler delegate that is used everywhere has the same signature so I would use that instead.
[Edit] When posting code, please place the code inside <pre></pre> tags. There is a widget available when posting that says "code block" that will help with this. Your code will then be much easier to read, therefore easier for us to help you!
|
|
|
|
|
The 1st class is static because it is my entry into the program
The 2nd class is static because it is constant used class, so I made it static if it should not please explain.
The 3rd class is actually a form and in reality is not a static class, I edited to read public.
You said to use "System.EventHandler" is that in place of a delegate, can you give me an example.
Thanks,
Michael
|
|
|
|
|
Yeah, what he said. Plus, you define DelC in both classes, that means that they are different delegates and will cause trouble. As with interfaces, when you declare a delegate, it's best to do so outside of any class.
|
|
|
|
|
I took out the 2nd reference to the DelC in the example and still have the same problem.
I can not set an instance of the delegate as follows:
FormC FC = new FormC();
DelC delC = new DelC(FC.buttonC_Click);
in class B because the project with Class B is downstream from the project Class FormC that is why I placed the above in Main and the reason I am using a delegate.
Thanks,
Michael
modified on Tuesday, June 7, 2011 10:09 PM
|
|
|
|
|
You haven't attached a handler for the event.
MAW30 wrote: Class B is downstream
Whatever that means.
How about this... in A...
DelC delC = new DelC (FC.buttonC_Click);
B.delC = new DelC (FC.buttonC_Click);
Once you instantiate B.
But it's a bad design.
modified on Tuesday, June 7, 2011 11:42 PM
|
|
|
|
|
When I said it was downstream I meant as far as references to projects.
I can't reference the project for class B, it will give a circular reference.
Thanks,
Michael
|
|
|
|
|
Then rethink the whole mess.
|
|
|
|
|
How can I get the title of this window and ensure it has certain specific text in it, to separate it from other notepad windows like it in C sharp? I have a program I'm trying to send controls to and the controls differ depending on the title and text of the window. I was using a DLL add on that will not function correctly in win7 because of the different way some GUI windows are drawn, so I wanted to make the entire app .net native.
Thanks for reading.
http://img683.imageshack.us/img683/3281/testgui1.jpg
|
|
|
|
|
There is no standard way to read window titles and text. Every platform (by this, I mean, C++/MFC, Winforms, WPF, etc) stores the title differently. Sometimes applications will store titles themselves if they are owner / custom drawing. As for getting the window text, thats equally impossible. Every control stores it differently. What about item based controls like ListView?
If all you care about is notepad.exe SPECIFICALLY, you can call Win32 GetWindowText() through interop on the window as well as the text box control inside to get it consistently across OSes.
|
|
|
|
|
Thanks for the response.
Notepad (as you guessed) was an example, I would like to be able to do this with any program I decide to build or interact with. Specifically this time, the program I am building now will interact with HP OpenView ticketing system. I believe it is written in Java? (an poorly written at that) and is obnoxious to try and interact with.
|
|
|
|
|
Actually upon further thought, window *title* is probably stored consistently across every app so it doesn't break Windows. The window *contents* on the other hand, you'd have to do that custom for each app pretty much. On some apps it may be *IMPOSSIBLE* to access the contents because they don't have to use native controls and as such, you have no access to that information. Also, keep in mind that string pointers are only valid in the address space of the application. If you look at how Spy++ works, it injects a DLL into the app you are spying on so it can access its pointers.
|
|
|
|
|
You can P/Invoke functions in User32.dll for this - untested, but these declarations should work.
I'll leave it to you how to work out how to use these to get the functionality you desire.
using System;
using System.Runtime.InteropServices;
using System.Text;
internal static class NativeMethods
{
public static bool SetWindowText(IntPtr hWnd)
{
return SetWindowText(hWnd, null);
}
[DllImport("User32.dll", SetLastError = true)]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
public static string GetWindowText(IntPtr hWnd)
{
int count = GetWindowTextLength(hWnd) + 1;
StringBuilder resultBuilder = new StringBuilder(count);
GetWindowText(hWnd, resultBuilder, count);
return resultBuilder.ToString();
}
[DllImport("User32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("User32.dll", SetLastError = true)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", SetLastError = true)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
}
Dave
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
modified on Tuesday, June 7, 2011 5:40 PM
|
|
|
|
|
Thank you, I will try that class and see if the getWindowText method can return what I need.
|
|
|
|
|
I screwed up with the last function and the delegate - I have eited my previous post
|
|
|
|
|
DaveyM69 wrote: I have eited my previous post
And are you going to eit that post as well?
|
|
|
|
|
Cheers Pete!
|
|
|
|
|
I've run a test with the code below and it works OK (a few tweaks to earlier code and an additional function).
using System;
using System.Runtime.InteropServices;
using System.Text;
internal static class NativeMethods
{
public static bool SetWindowText(IntPtr hWnd)
{
return SetWindowText(hWnd, null);
}
[DllImport("User32.dll", SetLastError = true)]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
public static string GetWindowText(IntPtr hWnd)
{
int count = GetWindowTextLength(hWnd) + 1;
StringBuilder resultBuilder = new StringBuilder(count);
GetWindowText(hWnd, resultBuilder, count);
return resultBuilder.ToString();
}
[DllImport("User32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("User32.dll", SetLastError = true)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", SetLastError = true)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("User32.dll", SetLastError = true)]
public static extern bool IsWindowVisible(IntPtr hWnd);
}
private List<IntPtr> windowHandles = new List<IntPtr>();
private bool EnumWindowsProcHandler(IntPtr hWnd, IntPtr lParam)
{
windowHandles.Add(hWnd);
return true;
}
NativeMethods.EnumWindowsProc enumWindowsProc =
new NativeMethods.EnumWindowsProc(EnumWindowsProcHandler);
NativeMethods.EnumWindows(enumWindowsProc, IntPtr.Zero);
foreach (IntPtr hWnd in windowHandles)
{
if (NativeMethods.IsWindowVisible(hWnd))
{
string caption = NativeMethods.GetWindowText(hWnd);
if (!string.IsNullOrEmpty(caption))
{
Console.WriteLine(caption);
if (caption.EndsWith("Microsoft Visual Studio"))
{
NativeMethods.SetWindowText(hWnd, "Changed VS Caption");
}
}
}
}
|
|
|
|
|
Either I throw exception or induce it naturally in DoWork it is not caught and control is not passed to worker completion routine instead getting that exception which I catch in Program.cs file
Type: System.Reflection.TargetInvocationException
Source: mscorlib
Message: Exception has been thrown by the target of an invocation.
Target Site: System.Object _InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.SignatureStruct ByRef, System.Reflection.MethodAttributes, System.RuntimeType)
Module Name: mscorlib.dll
Module Path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
Stack:
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
Чесноков
|
|
|
|
|
That's normal behavior. The exception doesn't get thrown across the thread boundary.
Of course, a background worker can return any object as a result, so when I use them (And I do, extensively), I do it like this:
private void SomethingBackground(object sender, DoWorkEventArgs e)
{
try
{
}
catch (SomethingSomethingException ex)
{
}
catch (Exception ex)
{
e.Result = ex;
}
}
if (e.Result is Exception)
throw (Exception)e.Result;
else
{
}
If you're doing this a lot, you could even make a generic wrapper to centralize the code... Plenty of options.
Nevermind... Not enough caffeine...
|
|
|
|
|
Will you argue with MSDN?
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx[^]
If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised. If you have more than one BackgroundWorker, you should not reference any of them directly, as this would couple your DoWork event handler to a specific instance of BackgroundWorker. Instead, you should access your BackgroundWorker by casting the sender parameter in your DoWork event handler
Чесноков
|
|
|
|
|