Solved

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

Posted on 2010-11-08
4
306 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

16 Experts available now in Live!

Get 1:1 Help Now