<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>words &#187; C#</title>
	<atom:link href="http://www.curlybrace.com/words/category/technology/net/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.curlybrace.com/words</link>
	<description>by Jeff Fitzsimons</description>
	<lastBuildDate>Tue, 20 Dec 2011 10:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Run External Application as Another User in C#</title>
		<link>http://www.curlybrace.com/words/2009/06/04/run-external-application-as-another-user-in-c/</link>
		<comments>http://www.curlybrace.com/words/2009/06/04/run-external-application-as-another-user-in-c/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 22:33:09 +0000</pubDate>
		<dc:creator>Jeff Fitzsimons</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.curlybrace.com/words/?p=737</guid>
		<description><![CDATA[An arbitrary external application can be executed from C# using System.Diagnostics.Process. If you want to run as another user, setting the System.Diagnostics.Process.StartInfo.Password field can be a bit confusing. Here is one way using System.Security.SecureString.AppendChar to avoid having to resort to &#8230; <a href="http://www.curlybrace.com/words/2009/06/04/run-external-application-as-another-user-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>An arbitrary external application can be executed from C# using <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx">System.Diagnostics.Process</a>.  If you want to run as another user, setting the System.Diagnostics.Process.StartInfo.Password field can be a bit confusing.  Here is one way using <a href="http://msdn.microsoft.com/en-us/library/system.security.securestring.appendchar(VS.85).aspx">System.Security.SecureString.AppendChar</a> to avoid having to resort to unsafe code:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSRunAs
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();

            // Domain and User Name:
            p.StartInfo.Domain = &quot;optional_domain&quot;;
            p.StartInfo.UserName = &quot;user_to_run_as&quot;;

            // Command to execute and arguments:
            p.StartInfo.FileName = &quot;c:\\path\\to\\executable.exe&quot;;
            p.StartInfo.Arguments = &quot;your argument string&quot;;

            // Build the SecureString password...
            System.String rawPassword = &quot;your_password&quot;;
            System.Security.SecureString encPassword = new System.Security.SecureString();
            foreach (System.Char c in rawPassword)
            {
                encPassword.AppendChar(c);
            }

            p.StartInfo.Password = encPassword;

            // The UseShellExecute flag must be turned off in order to supply a password:
            p.StartInfo.UseShellExecute = false;

            p.Start();
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.curlybrace.com/words/2009/06/04/run-external-application-as-another-user-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

