Showing posts with label csharp. Show all posts
Showing posts with label csharp. Show all posts

Friday, January 27, 2012

C# getting file names without extensions


When you walk a directory getting file names you get a file name with its extension: file1.txt, file2.txt, file3.txt.
How can you get file names without file extensions file1, file2, file3?

DirectoryInfo d = new DirectoryInfo(myDirectory);
FileInfo[] fileArrray = d.GetFiles("*.txt");
foreach (FileInfo f in smFiles)
{
   var fileNameOnly = Path.GetFileNameWithoutExtension(f.Name);
   
   //...
   //process file...
}

Thre is also a linq way:

var filenames = String.Join(", ", Directory.GetFiles(@"c:\", "*.txt")
.Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());

Submit this story to DotNetKicks

Wednesday, January 25, 2012

Jquery tabs postback problem and solution

I am using jquery library in my project and I use jquery-ui tabs.
When a postback occurs in any tab, tabs reloaded and goes to first tab.

<script type="text/javascript">
    $(document).ready(function() {
        $("#example").tabs();
    });
</script>

Solution is to store last tab in an hidden field and reload each time page loads.

<script language="javascript" type="text/javascript">
    $(function() {
        $("#example").tabs({
            show: function() {
                var sel = $('#example').tabs('select', $("#<%= hidLastTab.ClientID %>").val(sel));

            },
            selected: <%= hidLastTab.Value %>
         });
      });
</script>
<asp:hiddenfield id="hidLastTab" runat="server" value="0"></asp:hiddenfield>

or alternatively with a funcion:

private void SelectTab(int tabNumber) {
            var script = string.Format(@"$(document).ready( function(){{ $('.tabs').tabs( 'select', {0} ); }});", --tabNumber);
            ScriptHelper.RegisterHeadScriptBlock(typeof(System.Web.UI.Page), "TabSelector", script, true);

        }


Submit this story to DotNetKicks

Thursday, January 19, 2012

What is a Method Group?

A method group is the name for a set of methods (that might be just one).
The ToString function has many overloads - the method group would be the group consisting of all the different overloads for that function.
It is a compiler term for "I know what the method name is, but I don't know the signature"; it has no existence at runtime, where it is converted in the correct overload.
Also, if you are using LINQ, you can apparently do something like myList.Select(methodGroup).
so you can replace this code:
private static int[] ParseInt(string s)
{
    var t = ParseString(s);
    var i = t.Select(x => int.Parse(x));
    return i.ToArray();
}
with this one:
private static int[] ParseInt(string s)
{
    var t = ParseString(s);
    var i = t.Select(int.Parse);
    return i.ToArray();
}

Submit this story to DotNetKicks