Showing posts with label Window Forms Application. Show all posts
Showing posts with label Window Forms Application. Show all posts

Tuesday, February 19, 2013

Programmatically obtain a list of files contained in a directory

If you ever need to programmatically obtain information about files contained within a given directory/folder, I've provided some sample code from a Windows Form Application I created that will provide you with that capability.  In this particular instance, my Windows App allows the user to select the directory he/she wishes to review and then populates a Grid View control with information about the files contained in the directory (i.e. in this simple example, the name of the file and the date the file was last modified):

using System.Windows.Forms;
using System.Data;
using System.IO;

public partial class Form1 : Form
{
        private FolderBrowserDialog m_fbd;

        ...

        /// <summary>
        /// When the button is clicked, the user will be prompted to select the input file to open via the OpenFileDialog
        /// control.  Once the user has selected the input file, the information in the file will be read, parsed, and then
        /// displayed in the grid view control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReviewFiles_Click(object sender, EventArgs e)
        {
            if (m_fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FileName");
                dt.Columns.Add("LastModifiedDate");

                DirectoryInfo dir = new DirectoryInfo(m_fbd.SelectedPath);
                foreach (FileInfo file in dir.GetFiles("*.*"))
                {
                    DataRow row = dt.NewRow();
                    row["FileName"] = file.Name;
                    row["LastModifiedDate"] = file.LastWriteTime.ToString();
                    dt.Rows.Add(row);
                }
                dataGridView1.DataSource = dt;
            }
        }

If you would only like to display information about certain types of files, you can modify the parameter of the GetFiles method call appropriately.  In the following example, the code will only display information about  .pdf files contained in the selected directory:

foreach (FileInfo file in dir.GetFiles("*.pdf"))

Yes, I know...this example provides too much code to demonstrate how easy it is to programmatically obtain info about files in a given directory, but I thought it would be fun to add the additional code that could turn this into a nice utility if needed...

Thursday, October 11, 2012

Allowing a Windows Forms Application to manage SharePoint 2010

If you'd like to create a Windows Forms Application that a power user (i.e. perhaps a Business Analyst who isn't versed in PowerShell, but has a high level of access to the SharePoint Farm) could use for administering functionality on your SharePoint farm that isn't covered by standard tools, there are a few steps that you'll need to carry out first before your application will run on the farm.  After creating your Windows Forms Application, the first thing you'll need to do is add a reference to the Microsoft.SharePoint.dll file.  This can be accomplished via the following steps:
  1. Using Visual Studio 2010, open your Windows application solution
  2. Expand the tree under your solution in the Solution Explorer view
  3. Under your project, right-click on References and select Add Reference...
  4. There will be two routes you can pursue for adding the assembly based on your development environment:
  • If your developing on an environment with SharePoint installed, you can reference the assembly via the following steps:
    1. Click on the .NET tab
    2. Locate and click on the Microsoft.SharePoint item
    3. Click OK
  • If you don't have SharePoint installed in your environment, you can reference the assembly via the following steps:
    1. Obtain the assembly via the steps that I've covered in my blog post titled Accessing files contained in the Global Assembly Cache
    2. Create a new folder in your project called "External Assemblies"
    3. Add the Microsoft.SharePoint.dll file to the new folder
    4. Under your project, right-click on References and select Add Reference...
    5. Click on the Browse tab
    6. Navigate to the "External Assemblies" folder
    7. Click on the Microsoft.SharePoint.dll file
    8. Click OK
Once you've associated the SharePoint assembly, the next step will be to make certain your Windows application can run in a 64 bit environment if your app is going to be accessing SharePoint 2010.  By the way, if you don't follow these steps, your code will throw an error message that reads "Object reference not set to an instance of an object".  Here are the steps that will allow your app to run in a 64 bit environment: 
  1. In the Solution Explorer view, right-click on the project and select Properties
  2. Under General, locate the Platform target: dropdown and change the value from "x86" to "Any CPU"

  3. Save the change and build your solution
At this point in time, your Windows application will now be able to run directly on one of your SharePoint WFE servers in order to carry out the admin activities that your power user would like it to execute.