Solved

How do I write a C# condition statement within my Win Forms application?

Posted on 2010-11-08
4
307 Views
Last Modified: 2013-12-16
Hi,

I have the attached C# code within my win forms application...

The code basically writes specific data from a sharepoint site collection to an external file (FileSizeReport.csv) for review. This code works fine but I'm hoping to alter it so a user can type in a specific file name on the frontend form to filter the results written to the external file. I have created another variable called fileName which is set from an input textbox (FileName) on my frontend form. How can I alter my code to loop through each document library only returning files equal to or like the filename declared by the user?

Thanks.
private void GoFileChecker(object sender, EventArgs e)

        {            

            //Create output file

            TextWriter outputFile = new StreamWriter("FileSizeReport.csv");



            try

            {               

                //Get site collection url from textbox

                string siteCollectionURL = SiteCollURl.Text;

		string fileName = FileName.Text;		

                

                //Write column headers to the output file

                outputFile.WriteLine("Filename,Last Modified,Version Count,File Size (KB),Total Size (KB)");



                //Using the site collection URL, create a SPSite object

                using (SPSite siteColl = new SPSite(siteCollectionURL))

                {

                    //Foreach site in the collection, gather the required information

                    foreach (SPWeb web in siteColl.AllWebs)

                    {

                        try

                        {

                            SPListCollection webDocLibraries;



                            //Workout the name of the current site we are in

                            string webName = (string.IsNullOrEmpty(web.Name)) ? "" : web.Name + "/";



                            //Get a list of all the document libraries in the current web

                            webDocLibraries = web.GetListsOfType(SPBaseType.DocumentLibrary);

                            

                            //Foreach document library, get the matching folder object and process the files within

                            foreach (SPList docLib in webDocLibraries)

                            {

                                SPFolder docLibFolder = docLib.RootFolder;                                

                                ProcessSubDirectory(docLibFolder, outputFile);

                            }

                        }

                        finally

                        {

                            //Clean up the SPWeb object regarless if success or failure

                            if (web != null)

                            {

                                web.Dispose();

                            }

                        }

                    }

                }

                                                               

                ResultLabel.Text = "Complete";

            }

            catch

            {

                ResultLabel.Text = "Error";

            }

            finally

            {

                outputFile.Close();

            }

        }

Open in new window

0
Comment
Question by:aspnet-scotland
4 Comments
 
LVL 9

Expert Comment

by:puru1981
ID: 34086872
Here is some code in VB you can convert it into C#.

http://www.daniweb.com/forums/thread126159.html
0
 

Author Comment

by:aspnet-scotland
ID: 34088572
puru1982,

Thanks, but this doesn't meet my requirements as it just opens a file, I'm looking to loop through a sub set of data (sharepoint site collection) to return results filtered on a user defined string.

Thanks.
0
 
LVL 3

Expert Comment

by:JamesAperta
ID: 34089459
following assumes vFolder is an SPFolder.   will use RegEx to compare folder name against user entered string.   you might need to modify the vPattern depending on what the folder names look like

bool vMatch;
 
               // add a *? onto user string so that regular expression finds any match and is non-greedy
               // so stops as soon as we find a match
string vPattern = String.Format("{0}*?", vUserString);

vMatch = System.Text.RegularExpressions.Regex.IsMatch (vFolder.Name, vPattern);

if (vMatch)
    // file matches so do your work

0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 34089539
I'm not familiar with the SharePoint objects, so I'm gonna be a little fuzzy on the details...
I think you're gonna want to change the ProcessSubDirectory() method so that it accepts a third parameter, the file name to filter for, and change the loop in in ProcessSubDirectory() to check each file name...
I also used String.IndexOf to do a case in-sensitive check if the file name contains the the search keyword (RegEx's can be a pain in the butt, I try to avoid them when possible - they're also noticeably slower than the built-in String functions, which may or may not be important depending on how many documets you're searching)

void ProcessSubDirectory(SPFolder docLibFolder, TextWriter outputFile, string fileNameFilter)

{

	foreach(SPFile file in docLibFolder.Files)

	{

		if (StringContainsIgnoreCase(file.Name, fileNameFilter))

		{

			// this file name contains the filter file name,

			// do something with it...

		}

	}

}



// Returns true if s1 contains s2, otherwise false

bool StringContainsIgnoreCase(string s1, string s2)

{

	return s1.IndexOf(s2, StringComparison.InvariantCultureIgnoreCase) > -1;

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now