If there is a way, I want to know that the way to make extension method to System.IO.File in c#.  I want to make a method like below.  // 'CreateDirectoryAndFile is the method I made System.IO.File.CreateDirectoryAndFile("path");    but I've searched it on google, I can't find the way.  So, I was forced to try to do like below.  // I made a FileHelper class for this method. FileHelper.CreateDirectoryAndFile("path");   Isn't there a way to do like this?
21166
I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console.  For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go:  Console.WriteBlueLine("This text is blue");   I tried this by adding a local, public static method, with Console as a 'this' parameter... but no dice!      public static class Helpers {     public static void WriteBlueLine(this Console c, string text)     {         Console.ForegroundColor = ConsoleColor.Blue;         Console.WriteLine(text);         Console.ResetColor();     } }   This didn't add a 'WriteBlueLine' method to Console... am I doing it wrong? Or asking for the impossible?
104493
I have Library on which I have created a ribbon button. On click of the button I am trying to identify if the selected item is a folder or a file. below if the code snippet that I am using:  var context = SP.ClientContext.get_current(); var web = context.get_web();  var items = SP.ListOperation.Selection.getSelectedItems(context);  var currentLibrary = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList(context));  var index; for (index in items) {     var selectedItem = currentLibrary.getItemById(items[index].id);     // Need to identify if 'selectedItem' is folder or not     console.log("FileSystemObjectType = " + selectedItem.FileSystemObjectType); // Returns 'undefined'     console.log("get_file = " + selectedItem.get_file()); // Returns 'SP.ListItem' object     console.log("get_folder = " + selectedItem.get_folder()); // Returns 'SP.ListItem' object }   Taking reference from  I tried to use FileSystemObjectType but in my case it ends up showing undefined for both file and folder. Also if I try to check if the selected item is a file or folder using below code it always return true.  if(item.FileSystemObjectType == SP.FileSystemObjectType.Folder) {     // Always returns true }   What am I doing wrong here? How can I identify if the selected item in Library is a file or a folder?
101639
How can I copy the path to a folder or a file? The Info Dialog doesn't allow me to copy the information that stands there ...    Is there an easy way to do this?
30927
Ubuntu 16.04 I have 2 instances of Nautilus (file explorer) open - in different drives and or folders.  I want to create a LINK to a file.  It doesn't appear to be easily done - if I drag &amp; drop the file it makes a copy. If I right-click the file the menu does have a 'Make Link', but it makes the link in the folder the file already is in (why would I want that?) - and I have to then drag the link to the place I want, which makes a copy of the link, and then I have to delete the link made in the place I don't want. Seems like very inefficient way. So, I'm probably missing something....?
104015
Is there a built in method for waiting for a file to be created in c#?  How about waiting for a file to be completely written?  I've baked my own by repeatedly attempting File.OpenRead() on a file until it succeeds (and failing on a timeout), but spinning on a file doesn't seem like the right thing to do.  I'm guessing there's a baked-in method in .NET to do this, but I can't find it.
73724
There is a standard command for file splitting - split.  For example, if I want to split a words file in several chunks of 10000 lines, I can use:  split -dl 10000 words wrd   and it would generate several files of the form wrd.01, wrd.02 and so on.  But I want to have a specific extension for those files - for example, I want to get wtd.01.txt, wrd.02.txt files.  Is there a way to do it?
76849
I am trying to write a function to determine if a file exists. The two methods prove to return inconsistent results (fileExists() seems to provide accurate results, compared to isFileFound(), that returns false positives - i would have expected an exception when trying to create the instance).   protected bool isFileFound(string path, string fileName)     {         System.IO.FileInfo fi = null;          bool found = false;         try         {             fi = new System.IO.FileInfo(path + fileName);             found = true;         }         catch (Exception e)         {             baselogger.Fatal(e.Message + " " + e.StackTrace + " \n" + path + fileName);         }          return found;     }      protected bool fileExists(string path, string pattern)     {         bool success = false;          try         {             success = File.Exists(path + pattern);         }         catch (Exception e)         {             baselogger.Warn(e.Message + " " + e.StackTrace + " " + e.Source);         }          return success;     }   Neither seems to be able to resolve a UNC path of the following syntax: \\abcserver\c$\xyzfolder\foo.bar  Any idea why the unc path is failing for these methods would be greatly appreciated.
113473
I'm developing an application targeting .NET Framework 2.0 using C# for which I need to be able to find the default application that is used for opening a particular file type.  I know that, for example, if you just want to open a file using that application you can use something like:  System.Diagnostics.Process.Start( "C:\...\...\myfile.html" );   to open an HTML document in the default browser, or  System.Diagnostics.Process.Start( "C:\...\...\myfile.txt" );   to open a text file in the default text editor.  However, what I want to be able to do is to open files that don't necessarily have a .txt extension (for example), in the default text editor, so I need to be able to find out the default application for opening .txt files, which will allow me to invoke it directly.  I'm guessing there's some Win32 API that I'll need to P/Invoke in order to do this, however a quick look with both Google and MSDN didn't reveal anything of much interest; I did find a very large number of completely irrelevant pages, but nothing like I'm looking for.
50133
I would find it very convenient to be able to use cd with a file argument.  cd myDirectory/anyname.anyExtension would be equivalent to cd myDirectory/  What would be the best alias or function to achieve this behavior ?  EDIT: Sorry I didn't mention it in the 1st place: I use zsh
112890
I have always made a point of writing nice code comments for classes and methods with the C# xml syntax. I always expected to easily be able to export them later on.  Today I actually have to do so, but am having trouble finding out how. Is there something I'm missing? I want to go Menu->Build->Build Code Documentation, but there is no option to do that, there.
84430
I've written the following routine to manually traverse through a directory and calculate its size in C#/.NET:    protected static float CalculateFolderSize(string folder) {     float folderSize = 0.0f;     try     {         //Checks if the path is valid or not         if (!Directory.Exists(folder))             return folderSize;         else         {             try             {                 foreach (string file in Directory.GetFiles(folder))                 {                     if (File.Exists(file))                     {                         FileInfo finfo = new FileInfo(file);                         folderSize += finfo.Length;                     }                 }                  foreach (string dir in Directory.GetDirectories(folder))                     folderSize += CalculateFolderSize(dir);             }             catch (NotSupportedException e)             {                 Console.WriteLine("Unable to calculate folder size: {0}", e.Message);             }         }     }     catch (UnauthorizedAccessException e)     {         Console.WriteLine("Unable to calculate folder size: {0}", e.Message);     }     return folderSize; }    I have an application which is running this routine repeatedly for a large number of folders.  I'm wondering if there's a more efficient way to calculate the size of a folder with .NET?  I didn't see anything specific in the framework.  Should I be using P/Invoke and a Win32 API?  What's the most efficient way of calculating the size of a folder in .NET?
34841
I've recently searched how I could get the application's directory in Java. I've finally found the answer but I've needed surprisingly long because searching for such a generic term isn't easy. I think it would be a good idea to compile a list of how to achieve this in multiple languages.  Feel free to up/downvote if you (don't) like the idea and please contribute if you like it.  Clarification:  There's a fine distinction between the directory that contains the executable file and the current working directory (given by pwd under Unix). I was originally interested in the former but feel free to post methods for determining the latter as well (clarifying which one you mean).
103885
We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.  A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.  This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.
20538
I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class.  Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text").  Here's the syntax for creating an extension method for StringBuilder:  public static class sbExtensions {     public static StringBuilder blah(this StringBuilder sb)     {         return sb;     } }    It successfully adds the blah extension method to the StringBuilder.  Unfortunately, operator overloading does not seem to work:  public static class sbExtensions {     public static StringBuilder operator +(this StringBuilder sb, string s)     {         return sb.Append(s);     } }    Among other issues, the keyword this is not allowed in this context.  Are adding operator overloads via extension methods possible?  If so, what's the proper way to go about it?
98998
I need to do create a file with a filename such as :&gt;?, is this possible somehow? Windows stops it.
27910
I'm using .NET 3.5, trying to recursively delete a directory using:  Directory.Delete(myPath, true);   My understanding is that this should throw if files are in use or there is a permissions problem, but otherwise it should delete the directory and all of its contents.  However, I occasionally get this:  System.IO.IOException: The directory is not empty.     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)     at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)     at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)     ...   I'm not surprised that the method sometimes throws, but I'm surprised to get this particular message when recursive is true.  (I know the directory is not empty.)  Is there a reason I'd see this instead of AccessViolationException?
106043
I have a C# application that scans a directory and gathers some information.  I would like to display the account name for each file.  I can do this on the local system by getting the SID for the FileInfo object, and then doing:  string GetNameFromSID( SecurityIdentifier sid ) {     NTAccount ntAccount = (NTAccount)sid.Translate( typeof( NTAccount ) );     return ntAccount.ToString(); }   However, this does not work for files on a network, presumably because the Translate() function only works with local user accounts.  I thought maybe I could do an LDAP lookup on the SID, so I tried the following:  string GetNameFromSID( SecurityIdentifier sid ) {     string str = "LDAP://&lt;SID=" + sid.Value + "&gt;";     DirectoryEntry dirEntry = new DirectoryEntry( str );     return dirEntry.Name; }   This seems like it will work, in that the access to "dirEntry.Name" hangs for a few seconds, as if it is going off and querying the network, but then it throws a System.Runtime.InteropServices.COMException  Does anyone know how I can get the account name of an arbitrary file or SID?  I don't know much about networking or LDAP or anything.  There's a class called DirectorySearcher that maybe I'm supposed to use, but it wants a domain name, and I don't know how to get that either - all I have is the path to the directory I'm scanning.  Thanks in advance.