It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
is there any way to throw new exception on maxmium number of files in a directory ?
No, there is none:
you can catch it with a more generic
System.IO.IOException
And read the (underlying) message you could throw a self written Exception from that point.
UPDATE
I just learned . that you use the
bool isfull = info.GetFiles().LongLength == 4.294.967.295
property. Unfortunately it will eat all your memory.
Therefore using
DirectoryInfo.EnumerateFiles().Count()
perhaps in a chunked manner could be a better approach
fyi:
65,534 for FAT32
4,294,967,295 FOR NTFS
(source)
I don't know specifically about C#, but I know Java doesn't have a specific exception or error to throw for a full drive. I suggest simply making your own exception class and using that. I suggest calling it a FullDirectoryError or DirectoryOverflowError, assuming it's serious enough to be called an error.
Something like this?
public static void checkFileNumber(string directoryToCheck, int maxNumber )
{
if ( Directory.Exists( directoryToCheck ) )
{
if ( Directory.GetFiles( directoryToCheck ).Length > maxNumber )
{
throw new Exception("Too many files in " + directoryToCheck);
}
}
}
Be sure to use System.IO; :-)
Throwing exceptions is an expensive action; when you can avoid it, please do so. Try to program defensively if it is possible. But when throwing one, you have to supply a matching catch on a specific exception.
Like Casper Kleijne said, you could catch on IOException but why in the first place would you want to catch anyway ? What is the compensating action in the catch ?
Please supply some more info what you want to achieve with it.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
When I use the code below, it compiles but the rest of the code doesn't seem to work. When I take out the Substring part of it, it does.
-Steps
String theDate, theWeekDay;
if (ToTime(Time[0]) == ToTime(0, 0, 0))
{
theDate=ToDay(Time[0]).ToString().Substring(0,3);
theWeekDay=Time[0].DayOfWeek.ToString().Substring(4,8);
DrawTextFixed("day",theWeekDay, TextPosition.BottomRight);
DrawText("day"+Convert.ToString(ToDay(Time[0])),
theWeekDay+" "+theDate,0, Low[0]-TickSize*20, Color.Blue);
}
You haven't given enough information to solve your problem, but if you're just trying to get the day of the week name in the abbreviated format, use this instead:
theWeekDay = Time[0].ToString("ddd");
You're going to have to provide more than just this snippet of code. What is the Time object you're accessing via an indexer? Have you debugged this to see if Time[0] actually has a value? My guess here would be that Time[0] doesn't return a value that DayOfWeek can work with hence Substring(0,3) is being running against either an empty string or a null value
Unless you have omitted part of the code, your assignment does not take place within a class definition or a method.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem. I am making an app on Windows 8 which has a textbox. I want the textbox to remember it's value when I close the application.
There are many possible solutions on this,
One is by saving the value in the database or
by saving it on Settings.
You have several options:
Properties.Settings (and on form closing use Save)
Windows registry
Use storage file
But you'll have to do a little work by yourself.
For possible solutions, see other answers. Here I give you pseudo-code for storage file.
Create a callback that is called whenever application launches and one for then it closes.
When it launches, load your file, get all of it's text including \n in one string, and set that as text.
When it closes, get the text in the box including \n and write it to the file.
It is quite simple. I would prefer the storage file method here because:
Properties.Settings is slightly overkill, though it's a viable alternative.
Windows Registry will work but it won't be cross platform (you may not care about that but it's always a good idea to make sure your app can be ported, in case it's successful)
You can use the approach to save a value to a File.
Here is a very ugly exa,ple to get you started:
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Double val;
val = LoadDoubleValue();
Console.WriteLine("Previous Value was: " + val);
Console.Write("Please enter Value for next startup: ");
string input = Console.ReadLine();
if (Double.TryParse(input, out val))
{
SaveDoubleValue(val);
}
}
static Double LoadDoubleValue()
{
Double ret = 0;
if(File.Exists("save.dat")){
if (!Double.TryParse(File.ReadAllLines("save.dat")[0], out ret))
{
ret = 15;
}
}else{
ret = 15;
}
return ret;
}
static void SaveDoubleValue(Double val)
{
File.WriteAllLines("save.dat", new string[] { val.ToString() });
}
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to eval() and run this javascript code from my C# program, but it won't even debug.
How can I do this?
string jsFunc = "eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+c+'\\b','g'),k[c])}}return p}('8 4=\'6/13!)!6/12))6/19))))2!,!18*!16!15*!,!:14*-!17:9*!,!26***<\';8 5=\"\";20(3=0;3<4.24;3++){10(4.7(3)==25){5+=\'\\&\'}11 10(4.7(3)==23){5+=\'\\!\'}11{5+=21.22(4.7(3)-1)}};5;',10,27,'|||i|s|m|Nbui|charCodeAt|var||if|else|bct|spvoe|521|8477|_|73|2689|njo|for|String|fromCharCode||l{�ength|28|4451'.split('|')))";
JSEval.JSEval eval = new JSEval.JSEval();
string expression, result;
Console.Write("Выражение: ");
expression = jsFunc;
try
{
result = eval.Eval(expression).ToString();
}
catch
{
result = "!!!";
}
One potential problem, if I am permitted to hazard a guess based on the slim details available, is the odd character sequence found in the string:
...||l{�ength|28|4451'.split('|')))";
Perhaps you should remove the {� and re-run the code.
To elaborate on other meanings of the phrase "code don't debug":
Ensure the project is configured to build in Debug mode.
If your expectation is that you can step through the JavaScript, this will not be possible. You should instead debug the JavaScript using something like Firebug.
If you cannot mentally debug the JavaScript, because it has been minified, you should look at a tool to unpack the JavaScript into something more human readable.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Has been solved. Was trying to create a do statement that would not do anything until the while was checked. Was solved by simply putting an if statement before the do statement.
Using a do statement and anything else you might need, write the equivalent of:
while (b) {
s;
}
I think you might be looking for
if(b)
{
do
{
s;
}
while(b);
}
Is a standard part of the language:
do
{
s;
} while (b);
If I understood your question correctly you have a code that uses do..while loop and you want to replace it with a "regular" while loop.
That is not so simple as you say in your example. The difference is that do..while will always run at least once, and check the condition after it's completed. While loop may not run at all.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hi I have the following code:
medianListTester.cfyPE = 0;
if (medianListTester.cfyPE != 0 || testStock.getCEPS() != 0)
{
medianCYPE_price = medianListTester.cfyPE * testStock.getCEPS();
counter++;
}
else
//do something else
but it doesn't seem to "do something else" and still fires the code inside the conditional. Am I doing something wrong here? Why won't it run the code in the if statement?
If testStock.getCEPS() is not 0 you will not hit the else block. Depending on what you need and what you are expecting you might need to use && instead of || ?
medianListTester.cfyPE is set to 0, so the first clause in your if statement evaluates to false. What does TestStock.getCEPS()? If that is non-zero, then that will evaluate to true, and the entire if statement is then true.
I can't really tell what you're asking, but you're calling getCEPS() twice, and perhaps that is returning two different values, and perhaps that is causing your trouble.
Try this instead:
medianListTester.cfyPE = 0;
var ceps = testStock.getCEPS();
if (medianListTester.cfyPE != 0 || ceps != 0)
{
medianCYPE_price = medianListTester.cfyPE * ceps;
counter++;
}
else
//do something else
It sounds like testStock.getCEPS() is returning something other than 0. That would explain why it never hits do something else. Have you checked what this is returning?
What's the value of the call to testStock.getCEPS()? If that's not 0 it will still enter that code block. Also, I don't know the details of the object referenced by medianListTester, but it is possible that there is a getter/setter that isn't working right.
Your best bet for solving this is to print out or somehow look at the values of both medianListTester.cfyPE and testStock.getCEPS() right after the assignment but just before the if statement.