I have simple TextBox when the user can insert File path.  Now I want to validate if this File path is valid for example:  c:\blabla --&gt; this valid  c:  \blabla --&gt; this Not valid   I have this function:  public static bool ValidateFilePath(string path) {     FileInfo fileInfo = null;     try     {         fileInfo = new FileInfo(path);     }      catch (ArgumentException) { }     catch (PathTooLongException) { }     catch (NotSupportedException) { }     if (fileInfo is null)         return false;     else         return true; }   But this return True all the time.  Any suggestion or maybe ready c# method that can verify this ?
44359
I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+ but it doesn't include many national-specific characters from various languages (e.g. umlauts and so on). What is the best way to do such a check?
54431
I have a path and I need to determine if it is a directory or file.    Is this the best way to determine if the path is a file?  string file = @"C:\Test\foo.txt";  bool isFile = !System.IO.Directory.Exists(file) &amp;&amp;                           System.IO.File.Exists(file);   For a directory I would reverse the logic.  string directory = @"C:\Test";  bool isDirectory = System.IO.Directory.Exists(directory) &amp;&amp;                              !System.IO.File.Exists(directory);   If both don't exists that then I won't go do either branch.  So assume they both do exists.
35497
I am using the following method to browse for a file:      OpenFileDialog.ShowDialog()     PictureNameTextEdit.Text = OpenFileDialog.FileName   Is there a way get ONLY the file name?  The FileName method returns the entire path and file name.  i.e. I want Foo.txt instead of C:\SomeDirectory\Foo.txt
33175
true.ToString()  false.toString();  Output: True False   Is there a valid reason for it being "True" and not "true"? It breaks when writing XML as XML's boolean type is lower case, and also isn't compatible with C#'s true/false (not sure about CLS though).  Update  Here is my very hacky way of getting around it in C# (for use with XML)  internal static string ToXmlString(this bool b) {     return b.ToString().ToLower(); }   Of course that adds 1 more method to the stack, but removes ToLowers() everywhere.
59897
I need to send hundreds of newsletters, but would like to check first if email exists on server. It's called  validation, at least I think so, based on my research on Internet.    There's several libraries that can do that, and also a page with open-source code in  (), but I have hard time reading ASP Classic, and it seems that it uses some third-party library...    Is there some code for SMTP validation in C#, and/or general explanation of how it works?
76521
I need to validate an XML string (and not a file) against a DTD description file.   How can that be done in python?
37488
This works just fine:      protected void txtTest_Load(object sender, EventArgs e)     {         if (sender is TextBox) {...}      }   Is there a way to check if sender is NOT a TextBox, some kind of an equivalent of != for "is"?  Please, don't suggest moving the logic to ELSE{} :)
68619
Currently I have to manually remove the /error?aspxerrorpath= part from the address to try again. And right now a lot of Oopses happen!
104904
I really should be able to get this, but I'm just to the point where I think it'd be easier to ask.  In the C# function:  public static T GetValue&lt;T&gt;(String value) where T:new() {    //Magic happens here }   What's a good implementation for the magic? The idea behind this is that I have xml to parse and the desired values are often primitives (bool, int, string, etc.) and this is the perfect place to use generics... but a simple solution is eluding me at the moment.  btw, here's a sample of the xml I'd need to parse  &lt;Items&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO&lt;/ItemType&gt;         &lt;Name&gt;A Yamaha piano&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;false&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;         &lt;/properties&gt;        &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO_BENCH&lt;/ItemType&gt;         &lt;Name&gt;A black piano bench&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;false&lt;/allowCopy&gt;             &lt;url&gt;www.yamaha.com&lt;/url&gt;         &lt;/properties&gt;     &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;DESK_LAMP&lt;/ItemType&gt;         &lt;Name&gt;A Verilux desk lamp&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;             &lt;quantity&gt;2&lt;/quantity&gt;         &lt;/properties&gt;     &lt;/item&gt; &lt;/Items&gt;
114872
I'm loading an image from a file, and I want to know how to validate the image before it is fully read from the file.  string filePath = "image.jpg"; Image newImage = Image.FromFile(filePath);   The problem occurs when image.jpg isn't really a jpg.  For example, if I create an empty text file and rename it to image.jpg, an OutOfMemory Exception  will be thrown when image.jpg is loaded.  I'm looking for a function that will validate an image given a stream or a file path of the image.  Example function prototype  bool IsValidImage(string fileName); bool IsValidImage(Stream imageStream);
23927
I have a WPF dialog with a couple of textboxes on it. Textboxes are bound to my business object and have WPF validation rules attached.  The problem is that user can perfectly click 'OK' button and close the dialog, without actually entering the data into textboxes. Validation rules never fire, since user didn't even attempt entering the information into textboxes.  Is it possible to force validation checks and determine if some validation rules are broken?  I would be able to do it when user tries to close the dialog and prohibit him from doing it if any validation rules are broken.  Thank you.
53573
Basically, I would like to check if I have rights to open the file before I actually try to open it; I do not want to use a try/catch for this check unless I have to. Is there a file access property I can check before hand?
31495
How can someone validate that a specific element exists in an XML file? Say I have an ever changing XML file and I need to verify every element exists before reading/parsing it.
4416
What's the best way to validate that an IP entered by the user is valid?  It comes in as a string.
68167
I'm loading an XML document in my C# application with the following:  XDocument xd1 = new XDocument(); xd1 = XDocument.Load(myfile);   but before that, I do test to make sure the file exists with:  File.Exists(myfile);   But... is there an (easy) way to test the file before the XDocument.Load() to make sure it's a valid XML file?  In other words, my user can accidentally click on a different file in the file browser and trying to load, say, a .php file causes an exception.    The only way I can think of is to load it into a StreamWriter and simple do a text search on the first few characters to make sure they say "  Thanks!  -Adeena
40567
I'm working on a project where i find i'm checking for the following in many, many places:  if(item.Rate == 0 || item.Rate == null) { }   more as a curiousity than anything, what's the best way to check for both cases?  I've added a helper method which is:  public static bool nz(object obj) {     var parsedInt = 0;     var parsed = int.TryParse(obj.ToString(), out parsedInt);     return IsNull(obj) || (parsed &amp;&amp; parsedInt == 0); }   Is there a better way?
7845
I want to try to convert a string to a Guid, but I don't want to rely on catching exceptions (   for performance reasons - exceptions are expensive for usability reasons - the debugger pops up  for design reasons - the expected is not exceptional   In other words the code:  public static Boolean TryStrToGuid(String s, out Guid value) {     try     {         value = new Guid(s);         return true;     }     catch (FormatException)     {         value = Guid.Empty;         return false;     } }   is not suitable.  I would try using RegEx, but since the guid can be parenthesis wrapped, brace wrapped, none wrapped, makes it hard.   Additionally, I thought certain Guid values are invalid(?)    Update 1   had a good idea to catch only FormatException, rather than all. Changed the question's code sample to include suggestion.    Update 2  Why worry about thrown exceptions? Am I really expecting invalid GUIDs all that often?   The answer is yes. That is why I am using TryStrToGuid - I am expecting bad data.  Example 1 . I might be parsing folder names, checking to see if the text after the final . is a GUID.  c:\Program Files c:\Program Files.old c:\Users c:\Users.old c:\UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666} c:\Windows c:\Windows.old   Example 2 I might be running a heavily used web-server wants to check the validity of some posted back data. I don't want invalid data tying up resources 2-3 orders of magnitude higher than it needs to be.  Example 3 I might be parsing a search expression entered by a user.     If they enter GUID's I want to process them specially (such as specifically searching for that object, or highlight and format that specific search term in the response text.)    Update 3 - Performance benchmarks  Test converting 10,000 good Guids, and 10,000 bad Guids.  Catch FormatException:    10,000 good:     63,668 ticks    10,000 bad:   6,435,609 ticks  Regex Pre-Screen with try-catch:    10,000 good:    637,633 ticks    10,000 bad:     717,894 ticks  COM Interop CLSIDFromString    10,000 good:    126,120 ticks    10,000 bad:      23,134 ticks   p.s. I shouldn't have to justify a question.