Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Summary
Great tips for .NET developers
Description

How To Execute a File and Capture It's Output
You can use the Process class and capture the redirection output into the StandardOutput stream.

The following demonstrates:

public bool SetAcl()
{
    if (this.Pathname == null || this.Pathname == "")
    {
        ErrorMessage += "Path cannot be empty.";
        return false;
    }

    // *** Strip off trailing backslash which isn't supported
    if (this.Pathname.EndsWith("\\"))
        this.Pathname = this.Pathname.Substring(0, this.Pathname.Length - 1);

    string CommandLine = '"' + System.IO.Path.GetFullPath(this.Pathname) + '"' + " /C ";

    if (this.InheritSubDirectories)
        CommandLine += @" /T ";
    if (!this.OverrideExistingRights)
        CommandLine += @" /E ";

    CommandLine += @" /P " + this.Username + ":" + this.UserRights;

    //System.Windows.Forms.Clipboard.SetDataObject( CommandLine );

    Process p = new Process();
    p.StartInfo.FileName = "cacls.exe";
    p.StartInfo.Arguments = CommandLine;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
    p.StartInfo.CreateNoWindow = true;

    
    p.Start();

    p.StandardInput.Write("Y\r\n");
    string Response = p.StandardOutput.ReadToEnd();

    // *** If no output gets generated there's an error
    if (Response == null || Response == "")
    {
        this.ErrorMessage += "Unable to set permissions on " + this.Pathname + _
         " for " + this.Username;
        return false;
    }

    //System.Windows.Forms.MessageBox.Show( Response);

    return true;
}

from a solution provided by Rick Strahl in Message #1080891

Re-arrange Columns In A DataSet
Sometimes the need may arise to have to re-arrange columns in a DataSet.

If you're using ADO.NET 2.0 then you can change the position of a column using the DataColumn.SetOrdinal() method.

MyDataSet.Tables["MyTable"].Columns["MyColumn"].SetOrdinal(5);

If you're using ADO.NET 1.1 then you will have to code a method to spin through the data and build a copy with the columns in the desired order.
DataSet dsNew = new DataSet();
DataTable dt = new DataTable("MyTableName");
DataColumn OldCol;

// Swapping columns 0 and 1
OldCol = MyDataSet.Tables[0].Columns[1];
dt.Columns.Add(new DataColumn(OldCol.ColumnName, OldCol.DataType));
OldCol = MyDataSet.Tables[0].Columns[0];
dt.Columns.Add(new DataColumn(OldCol.ColumnName, OldCol.DataType));

dsNew.Tables.Add(dt);

from a solution provided by Michael McLain in Message #1117263

Browser's Open/Save Dialog
From a Web application, here's how to save a buffer full of bytes to a file by presenting the user with the Open/Save Dialog:
long nLen = 0;     // bytes returned from file extraction
byte[] bBuffer = oUsers.readBLOB(sFileName, ref nLen);
if (nLen > 0)
{
    Response.Clear();
    Response.ContentType = "application/download";
    Response.BufferOutput = true;
    Response.AddHeader("Content-Disposition", "attachment;filename=" + sFileName);
    Response.AddHeader("Content-Length", nLen.ToString());
    Response.BinaryWrite(bBuffer);
    Response.Flush();
}

from a solution provided by Alex Feldstein in Message #1147607

Passing Parameters during Debugging
When running an application that requires command line parameters, everyone knows how to run the .EXE and pass parameters. But, how do you do it from Visual Studio while debugging?

First, let's see how to set up your app to allow parameters in the first place.

[STAThread]
static void Main(string[] parms)
{
	if (parms.Length > 0)
	{
		// do something with the parameters
	}
	Application.Run(new Form1());
}

And now to be able to debug with parameters, right-click on your Windows Application project and choose Properties. Go to the "Debug" tab, and enter your parameters in the "Command Line Arguments" TextBox.

Note that the parameters are space-delimited.

Also note that you must either tab out of the TextBox or otherwise leave the TextBox (mouse-click) after you've made any change to the Command Line Arguments, ... you'll get an error if you don't (I'd say that's a bug, but it's only a minor annoyance if you forget to do it).

from a solution provided by Eric Moreau in Message #1065158

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers