Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

c#: crawler project here is the Answer:

Hi,
Could I get very easy to follow code examples on the following:
  1. Use browser control to launch request to a target website.
  2. Capture the response from the target website.
  3. convert response into DOM object.
  4. Iterate through DOM object and capture things like "FirstName" "LastName" etc if they are part of response.
thanks

Answer is:


Here is code that uses a WebRequest object to retrieve data and captures the response as a stream.
    public static Stream GetExternalData( string url, string postData, int timeout )
    {
        ServicePointManager.ServerCertificateValidationCallback += delegate( object sender,
                                                                                X509Certificate certificate,
                                                                                X509Chain chain,
                                                                                SslPolicyErrors sslPolicyErrors )
        {
            // if we trust the callee implicitly, return true...otherwise, perform validation logic
            return [bool];
        };

        WebRequest request = null;
        HttpWebResponse response = null;

        try
        {
            request = WebRequest.Create( url );
            request.Timeout = timeout; // force a quick timeout

            if( postData != null )
            {
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;

                using( StreamWriter requestStream = new StreamWriter( request.GetRequestStream(), System.Text.Encoding.ASCII ) )
                {
                    requestStream.Write( postData );
                    requestStream.Close();
                }
            }

            response = (HttpWebResponse)request.GetResponse();
        }
        catch( WebException ex )
        {
            Log.LogException( ex );
        }
        finally
        {
            request = null;
        }

        if( response == null || response.StatusCode != HttpStatusCode.OK )
        {
            if( response != null )
            {
                response.Close();
                response = null;
            }

            return null;
        }

        return response.GetResponseStream();
    }
For managing the response, I have a custom Xhtml parser that I use, but it is thousands of lines of code. There are several publicly available parsers (see Darin's comment).
EDIT: per the OP's question, headers can be added to the request to emulate a user agent. For example:
request = (HttpWebRequest)WebRequest.Create( url );
                request
.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, */*";
                request
.Timeout = timeout;
                request
.Headers.Add( "Cookie", cookies );

               
//
               
// manifest as a standard user agent
                request
.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US)";
 

Force local user to change password at next login with C#

I'm writing a function for a web app in ASP.NET where the client logs into the server machine, which is Windows authenticated against the local users on the server. The function I am writing resets the users password and emails them the new one. I do this like so:
String userPath = "WinNT://"  + Environment.MachineName + "/" + username.Text;
DirectoryEntry de = new DirectoryEntry(userPath);
de.Invoke("SetPassword", new object[] { password });
How can I also check the flag to force the user to change their password the next time they log in with the password emailed to them? I tried using pwdLastSet like so:
de.Properties["pwdLastSet"].Value = 0;
But this apparently only works with LDAP, not WinNT, and I am doing this locally.
Any experts know any better than me? I have even tried looking for a way to do this through the command line so that I can just create a Process, but I haven't been able to find a way to do it that way, either.


Answer is:

For WinNT, you must set the value to 1 rather than 0, and the property name is "PasswordExpired" rather than "pwdLastSet"; see http://msdn.microsoft.com/en-us/library/aa746542(VS.85).aspx
In other words, do this for WinNT:
de.Properties["PasswordExpired"].Value = 1;
(It is confusing, I know, but for LDAP you need to set the property "pwdLastSet" to 0. How's that for inconsistency!)

Get DisplayName Attribute without using LabelFor Helper in asp.net MVC

What is the best way to retrieve the display name attribute for an item in your model? I see a lot of people using the LabelFor helper for everything, but a label isn't appropriate if I just want to list the data out. Is there an easy way just get the Name Attribute if I just want to print it out in, say a paragraph?

Answer is"


<p>
    <%= Html.Encode(
        ModelMetadata.FromLambdaExpression<YourViewModel, string>(
            x => x.SomeProperty, ViewData).DisplayName
    ) %><p>
Obviously in order to avoid the spaghetti code it is always a good idea to write a helper:
public static class HtmlExtensions
{
    public static MvcHtmlString GetDisplayName<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper, 
        Expression<Func<TModel, TProperty>> expression
    )
    {
        var metaData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
        string value = metaData.DisplayName ?? (metaData.PropertyName ?? ExpressionHelper.GetExpressionText(expression));
        return MvcHtmlString.Create(value);
    }
}
And then:
<p>
    <%: Html.GetDisplayName(x => x.SomeProperty) %></p>

Improve the design of GUI/website [closed]

So i am using visual studio 2008, asp.net 3.5, with the basic toolkit provided.
Now i have made the gui which a lot of functionality but the design is very basic. and looks too old.
I need to give it a new look, a new feeling new designs....
like the gridview, the buttons the textboxes, the menus look basic... this is not working for me.
Please let me how should i go about doing this.?? 1) i have herd about tool kits but dont kno which ones are good..(dont want the really expensive ones) but if it is really good my company is ready to spend.
2) will the new VS 2010 or asp.net 4.0 make a difference.
3) The ajax toolkit or silverlight toolkit is any good?
4) i also need to show Charts and graphs now, currently using MS charts.. but now i need which is good.

Answer is:


Your best bet is to ask very specific questions at a more appropriate forum.
For ideas on designs, look for examples online and do something similar to what you like.
http://www.thecssawards.com/
http://www.csselite.com/
For questions on how to implement a specific design in html/asp.net/whatever, post a very specific question here.
For UI guidance on how to make something specific look better, post a question on http://ui.stackexchange.com. Include a SMALL screen shot of the applicable controls (not the whole page, just the part you're asking about, or at least highlight the part you're asking about).
.NET 3.5 vs .NET 4 will have no real effect on the design of your site. Whether your choose HTML or Silverlight will have a huge effect, but neither is generally better for all sites and switching between them basically means rewriting everything, so you wouldn't do it just for design reasons.

c# ASP.NET Controls collections code block nonsense.

What can cause:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
I ask because, the code does not contain code blocks. Anywhere. There is not a single <%= %> block on any page in the entire site. All i have are the <%@ %> directive lines.
Edit:
Below is the code causing the error.
/// <summary>
/// Adds javascript variables to a page so that ASP control ID are accessable via Javascript. The
/// variable names are ASP_ControlID.
/// </summary>
/// <param name="page">The Page to add the javascript to</param>
/// <param name="ctls">The controls to make accessable</param>
public static void addJavascriptIDs(Page page, params Control[] ctls)
{
    Literal litJS = new Literal();
    litJS.Text = getJavascriptIDs(ctls);
    page.Form.Controls.Add(litJS);  ////////// <-- Error Here
}

How? static members inside non-static classes and garbage collection

A collegue of mine claims that in C# having static members in non-static classes prevents instances of those classes from ever being garbage collected and that this is a common source of C# memory leaks. As a result he always wraps static members in a static class and gains access to them from there via a static property or method(s) on that static class. I always thought that statics were on the stack, not the heap and so did not have anything to do with the garbage collection. It does not seem right to me.
What is the truth about this?

Answer is

He doesn't know what he's talking about. Static members inside a non-static class do not prevent instances of the class from being garbage collected.
That said, statics can be on the stack or heap. It doesn't matter for garbage collection. What does matter is that the static parts of a type are not stored with instances of the type.

I have the following code which uses the System.Timers.Timer How to dispose timer

// an instance variable Timer inside a method
Timer aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
aTimer.Interval = 300000;
aTimer.AutoReset = false;
aTimer.Enabled = true;
while (aTimer.Enabled)
{
    if (count == expectedCount)
    {
        aTimer.Enabled = false;
        break;
    }
}
And I have the following code to handle the event:
private static void OnElapsedTime(Object source, ElapsedEventArgs e)
{
    // do something
}
The question is: if the timer event gets triggered and enters the OnElapsedTime, would the Timer object stops and be properly garbage collected? If not, what can I do to properly dispose of the Timer object/stop it? I don't want the timer to suddenly creep up and cause havoc in my app.

Answer is:


Call Timer.Dispose: http://msdn.microsoft.com/en-us/library/zb0225y6.aspx
private static void OnElapsedTime(Object source, ElapsedEventArgs e)
{
    ((Timer)source).Dispose();
}

I'm maintaining an application that consumes and I Locking a static field

I'm maintaining an application that consumes and I Locking a static field a common library that has a static instance of a class(ClassWrapper). This class is a basically a wrapper around the Microsoft patterns and practices's CacheManager (v3.1 of the EL)
The library is hosted in a web application and also in a windows service app, (both are inherently multi threaded) There are a bunch of places in the app that invokes an Add method on this wrapper which in turn calls the Add on the cache manager to add an item to the cache manager.
As I understand, the CacheManager is not thread safe and the CacheWrapper does not perform any locking to ensure thread safety in the call to Add.
I cannot modify the library code directly to add synhronization code and am considering writing a helper method like so and modifying all call sites to use this helper instead of calling the Add on the wrapper directly.

class CacheHelper
{
    private static object _syncLock = new object();

    public static void Add<T>(CacheWrapper wrapper, string key, T value, int expireInMins)
    {
        lock (_syncLock)
        {
            wrapper.Add(key, value, expireInMins);
        }
    }
}

Do you see any problems with this approach. I'm a bit weary since the CacheWrapper is static and hence inherently so is the _syncLock. I feel a bit uneasy locking on static objects, but I don't have much of a choice since the CacheWrapper is a static instance exposed all throughout the process space of the host (web app and the windows service).
Any advice or vote of confidence would be much appreciated.

Answer is:
I am not sure about CacheManager being not thread-safe. Check this MSDN article - it states clearly:
Every method call made through the CacheManager object is thread safe.
Now, coming to your implementation, I am not certain why you are passing the CacheWrapper instance to your methods. CacheWrapper being static instance you can refer it directly such as
class CacheHelper
{

    private static CacheWrapper GetWrapper()
    {
       return [Lib Namespace].[Class Name].[Field Name referring to CacheWrapper];
    }


    public static void Add<T>(string key, T value, int expireInMins)
    {
        var wrapper = GetWrapper();
        lock (wrapper)
        {
            wrapper.Add(key, value, expireInMins);
        }
    }

    ...

What is Question: Swap two variables without using ref/out in C#

 Swap two variables without using ref/out in C#

Is it possible to swap two variables without using ref/out keyword in C#(i.e. by using unsafe)?
for ex,
        swap(ref x,ref y);//It is possbile to by passing the reference
But, Is there any other way of doing the same thing. In the swap function you can use a temp variable. But, How to swap the variables without using ref/out keywords in C#?

Answer is:

No, it's not possible to affect variables* in the caller without the use of ref or out. You could affect class members, but how would you know which ones you were being asked to swap?
*(You can affect instances of reference types and the changes will be visible to the caller, but instances are not "variables".)

Her is Question: Why do standard libraries for C# need both an assembly reference and an import?

Her is Question: Why do standard libraries for C# need both an assembly reference and an import?
What is the reason that I have to do both of these for the standard libraries? For every other language that I have used, I only have to do one to access standard libraries.

An assembly reference makes the code in the assembly available to another assembly. A using directive makes the types of another namespace available to a given source file. These are completely separate concepts. If you were inclined, you could get away with never using the usings directive and using fully-qualified type names. Similarly, it is possible to use the usings directive to refer to other namespaces within the same assembly.
Also, there is a perfect analog between assemblies/jars and usings/import between Java and C# for the purposes of this discussion. So C# is hardly unique here.

Here the Question: Converting from an int to a bool in C#

int vote;
Insertvotes(objectType, objectId , vote, userID); //calling
For this method call, I want to convert vote to a bool. How can I convert it?
Here is the method signature:
 public static bool Insertvotes(int forumObjectType, 
                                int objectId,
                                bool isThumbUp, 
                                int userID) {
    // code...
}

Answers IS:

You can try something like
Insertvotes(objectType, objectId , (vote == 1), userID); //calling
Assuming that 1 is voted up, and 0 is voted down, or something like that.