Blog Archives

Change ReadOnly File Attribute in C#

Keep in mind when deleting files from code the typical override allowing users to delete anything in their path, whether it is marked readonly or not, will prevent your IO process from continuing and throw a nice little generic “access denied” error.

To resolve this, simply remove the readonly attribute on the file prior to deletion.

     if (File.GetAttributes(fullfilename) == FileAttributes.ReadOnly)
                    {
                        File.SetAttributes(fullfilename, FileAttributes.Normal);
                    }

                    File.Delete(fullfilename);

Alternatively, using FileSystemInfo (unmodified courtesy of Ken White):

private static void DeleteFileSystemInfo(FileSystemInfo fsi)
{
    fsi.Attributes = FileAttributes.Normal;
    var di = fsi as DirectoryInfo;

    if (di != null)
    {
        foreach (var dirInfo in di.GetFileSystemInfos())
            DeleteFileSystemInfo(dirInfo);
    }

    fsi.Delete();
}

References
Wordpress (Imran Akram), http://imak47.wordpress.com/2009/01/15/how-to-remove-readonly-attribute-from-a-file/
StackOverflow (Ken White), http://stackoverflow.com/questions/667381/programatically-run-cmd-exe-as-adminstrator-in-vista-c

Advertisement

SVN Revision and Source Control

What is revision control, a.k.a source control?
At it’s core, what this term really means is a way for you to track changes that you may make over time to a particular file.

Each time you change the file and save/commit the change, it is stored as a transaction so you can easily roll back any changes you made at a later date, or search through the history to see something that may have been previously deleted or functional before recent changes.

Business documents usually contain multiple revisions, and knowing if you have the latest copy of a document floating around can be a headache if it changes frequently and the only form of distribution in your organization is email.

Source control will allow you to store all changes from multiple people in a single location, with intact historical reference to each change that is made.

Some source control systems are very popular, such as Microsoft VSS, TFS, SVN and CVS, however, many alternatives exist. There even exists a wikipedia page dedicated to listing a comparative chart of many different types.

After using many of these varieties, I have come to the conclusion that TFS and SVN are roughly equivalent in terms of enterprise scalability and functionality, and also the best overall solutions for development teams IMO. (I may post the proof and comparisons at a later date, too much for this post).

Right out of the box, SVN may not seem like much. To achieve the same functionality you get in a complete TFS installation, alot of configuration and installation must be done, but the end result is much easier to use in an SVN environment.

My recommended setup for SVN on Windows is as follows:
Tortoise SVN, http://tortoisesvn.tigris.org/
Ankh SVN, http://ankhsvn.open.collab.net/
Subversion Edge (SVN CLI), http://www.collab.net/downloads/subversion/
SVN Notifier, http://svnnotifier.tigris.org/
Good .Net API Reference, http://svnexpert.svn.sourceforge.net

Tortoise is a good tool for browsing the SVN repository and keeping your local folders in sync. Ankh is a Visual Studio plugin which allows for easy syncing directly from your IDE which is a nice feature esp. if migrating from a MS source control variant. Collabnet Subversion Edge is a good lightweight CLI if you are curious on all of the options available in SVN, or need to script something out automated. SVN Notifier is a good little utility that runs as a service and notifies you when updates occur from other team members. It also serves to automatically keep your files in sync as they are updated.

References
Wikipedia, “Comparison of revision control software”, http://en.wikipedia.org/wiki/Comparison_of_revision_control_software
Wikipedia, “Revision Control”, http://en.wikipedia.org/wiki/Revision_control
Wikipedia, “Apache Subversion”, http://en.wikipedia.org/wiki/Apache_Subversion
Wikipedia, “Microsoft Visual Source Safe”, http://en.wikipedia.org/wiki/Microsoft_Visual_SourceSafe
Wikipedia,”Concurrent Versions System”, http://en.wikipedia.org/wiki/Concurrent_Versions_System
Wikipedia, “Team Foundation Server”, http://en.wikipedia.org/wiki/Team_Foundation_Server

URL Encode in .Net

C#: (ASP .Net)

System.Web.HttpUtility.UrlEncode()

C#: (Client/Server Environment)

Uri.EscapeUriString()

References
MSDN, WebUtility Classhttp://msdn.microsoft.com/en-us/library/system.net.webutility.aspx
MSDN, Uri Classhttp://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx
MSDN blogs, http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx