Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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