?
Solved

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

Posted on 2010-11-08
4
Medium Priority
?
311 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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