Solved

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

Posted on 2010-11-08
4
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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