|
Hi all. I have a read/write project. In this part, I'm getting ready to read from the file, but before I do that, I check to make sure the fileNamePath exists, and if it doesn't I use my default path to get the filename from. So, I'm passing a fileNamePath to my method and I want to check if the directory exists before I use it.
My fileNamePath for this test is defined as an empty string, @"", so it doesn't exist. When I create my DirectoryInfo with the path, it's throwing the System.ArgumentException. This is how I create it:
DirectoryInfo di1 = new DirectoryInfo(fileNamePath);//throws exception
After this, I would do this:
if(di1.Exists) //but it doesn't get this far.
Any ideas what the problem could be? I just need to detect that the directory doesn't exist so I can use my default dir. I've tried
myTempDir = Path.GetDirectoryName(fileNamePath);
if(Directory.Exists(myTempDir)
but that had issues too.
Thanks!!
|
|
|
|
|
Use Directory.Exists("") instead.
Reread your message.
MichCl wrote: new DirectoryInfo(fileNamePath);//throws exception
Yes it will. The path doesn't exist so you can't very well get any directory info for it.
MichCl wrote: if(Directory.Exists(myTempDir)
Why would this have issues?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
modified 23-Mar-12 11:15am.
|
|
|
|
|
When I tried using that (I am typically handling a full file/name path, such as "C:\backup\2012\our project\FileIO\FileIO\bin\Test\ReadMe.txt", at this point) it's not getting past the part where I extract the directory from the path before I check Directory.Exists with the empty string:
myTempDir = Path.GetDirectoryName(fileNamePath);
Any thoughts on this?
Thanks!!!!
|
|
|
|
|
string fileNamePath = @"C:\backup\2012\our project\FileIO\FileIO\bin\Test\ReadMe.txt";
string myTempDir = Path.GetDirectoryName(fileNamePath);
if (!Directory.Exists(myTempDir))
{
Console.WriteLine("Hi");
}
Works fine for me. Can you post the actual code that you are using?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
It's when fileNamepath is @"" that the problem with using Path.GetDirectoryName happens. Maybe I'll catch the exception and if it happened, have them choose the directory or use my default dir.
|
|
|
|
|
You are doing things a little backwards.
Try this instead
if( Directory.Exists( fileNamePath ) )
{
DirectoryInfo di1 = new DirectoryInfo(fileNamePath);
}
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
I did that and didn't have to catch the extra exception! Much cleaner that way.
|
|
|
|
|
Glad I could help.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
MichCl wrote: Any ideas what the problem could be?
The short answer is poor documentation, i.e. not mentioned on MSDN.
The constructor will reject bad paths and if there is the potential for a problem I think the only solution is to do some validation on the argument. Directory.Exists must be the method of choice here as it seems to be quite resilient and returns false for paths which would cause DirectoryInfo to throw.
Bad paths may result from:
Illegal characters (this one is documented)
new DirectoryInfo(@"*");
Path format not supported. This has it's own undocumented exception which caught me out, once! Why this isn't reported as an ArgumentException is beyond me.
di = new DirectoryInfo(@"\c:\");
and the one you've found
di = new DirectoryInfo(@"");
Alan.
|
|
|
|
|
Maybe I'll just catch the exception and use the default dir in that case or bring up the Directory Browser.
|
|
|
|
|
Yes that would be the most practical solution.
I've just been looking at the FileInfo constructor documentation and to be honest MS could do themselves a favour if they copied some of the info over to the DirectoryInfo page as it mentions empty paths and the NotSupportedException for embedded ':'.
Alan.
|
|
|
|
|
get any API for get count number of hits occurred for a particular keyword date wise from Gnews,facebook and twitter.
|
|
|
|
|
..there's not a unified api available that allows you to search all these sites in a single call. You'll have to make an interface to each site, using the SDK or API's provided by them.
Facebook[^]
Twitter[^]
Google News doesn't provide an API (it's not required). but there are feeds available for non commercial use.
Bastard Programmer from Hell
|
|
|
|
|
I am making a media player. so I need tool bar and seek bar. how I get these. thanks.
|
|
|
|
|
Are you using Win Forms? Just add a toolbar and add buttons to them. What issues are you having?
|
|
|
|
|
thanks.I need seek bar and volume bar as like in new vlc player.
|
|
|
|
|
|
|
|
that is a nice link ...all code and steps are given...
|
|
|
|
|
|
|
I'm trying to create a ListBox on the fly without using any XAML code. I got it working - almost. What I have is an array of TextBoxes of 'Zone Descriptions'. I can put those into the ListBox as ListBoxItems, and it works great - no problem.
I want to add at the front of the TextBox a TextBlock or header that says 'Zone 1' or whatever the zone is along with the TextBox, which is user changeable. I have an array of TextBlocks (I probably could have had strings just as well). But, I can't (or don't know how to) adjust the ListBoxItem.Content to hold both the TextBlock and TextBox combo. LBI[i].Content = TBlkZoneDesc[i] + TBxZoneDesc[i] is what I want to do, but that syntax is not allowed. Doing either one separately works fine, but not both.
Do anyone have a suggestion on how to do that?
ListBox LBZoneDesc = new ListBox();
LBZoneDesc.Width = 300;
LBZoneDesc.Height = 240;
Grid.SetColumn(LBZoneDesc,0);
Grid.SetRow(LBZoneDesc,2);
ListBoxItem[] LBI = new ListBoxItem[64];
for (byte i = 0; i < 5; i++)
{
LBI[i] = new ListBoxItem();
LBI[i].Padding = new Thickness(2);
// LBI[i].Content = TBlkZoneDesc[i];
LBI[i].Content = TBxZoneDesc[i];
LBZoneDesc.Items.Add(LBI[i]);
}
C0Grid.Children.Add(LBZoneDesc);
|
|
|
|
|
Hello,
I have an issue in regular expression matching...
string x = "user = 'sa' password='eX65dere' server = 'localhost'";
Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}
while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_.
the following will work fine, when i try to add _ to the expression the exception occurs.
Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");
Can anyone help me on this?
|
|
|
|
|
I have solved this by changing the expression as follows...
Regex rx = new Regex(@"password='([a-zA-Z0-9\\%^&_*]+)\'");
Regards
Sebastian
|
|
|
|