|
you might be lucky and find the solution here[^]; make sure to read all of it though.
|
|
|
|
|
Greetings Gurus,
While enumerating the drives in my machine I am trying to get the output to print "No Label" if the VolumeLabel value is null / length = 0.
When I run the app, after the C: and D: respond, I keep getting an exception "the device is not ready" if I use the "No Label" qualifier, but the code runs fine with that bit commented out.
What syntax should I be using please?
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.VolumeLabel.Length == 0)
d.VolumeLabel = "No Label";
string label = d.VolumeLabel;
if (d.IsReady == true)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.AvailableFreeSpace / 1024000000);
Console.WriteLine(d.TotalFreeSpace / 1024000000);
Console.WriteLine(d.TotalSize / 1024000000);
}
}
|
|
|
|
|
My bad, order was wrong. Should read
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
if (d.IsReady == true)
{
string label = d.VolumeLabel;
if (label.Length == 0)
d.VolumeLabel = "No Label";
{
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.AvailableFreeSpace / 1024000000);
Console.WriteLine(d.TotalSize / 1024000000);
}
}
}
|
|
|
|
|
Try this instead:
string label = d.VolumeLabel;
if (label.Length == 0)
label = "No Label";
{
Console.WriteLine(label);
What was the point of assigning to label if you weren't going to use it for anything other than a lenght test?
|
|
|
|
|
Of course you're getting that exception if you ask the volume label of a drive that doesn't have any media inserted like a DVD, CD, BlueRay drive for instance:
DriveInfo[] allDrives = DriveInfo.GetDrives();
long divisorForGiga = 1024 * 1024 * 1024;
foreach (DriveInfo d in allDrives)
{
String label = d.VolumeLabel;
if (d.IsReady == true)
{
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0)
{
d.VolumeLabel = "No Label";
}
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.AvailableFreeSpace / divisorForGiga);
Console.WriteLine(d.TotalFreeSpace / divisorForGiga);
Console.WriteLine(d.TotalSize / divisorForGiga);
}
else
{
Console.WriteLine("Drive {0}: is not ready! Maybe there's no media inserted.")
}
}
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
I'm going to change your life (just like mine was a few years back...)
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) {
Can be written as this:
if (string.IsNullOrEmpty(d.VolumeLabel)) {
|
|
|
|
|
And that nice new version
if (string.IsNullOrWhitespace(" "))
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
string sayii = textBox1.Text.Substring(0, textBox1.Text.Length);
uint[] sayicc = new uint[textBox1.Text.Length];
for (int i = 0; i < textBox1.Text.Length; i++)
{
sayicc[i] = Convert.ToUInt32(sayii[i] - 48);
}
uint[] dizi2 = new uint[textBox1.Text.Length];
uint bit = 1;
for (int j = 0; j < textBox1.Text.Length; j++)
{
dizi2[j] = bit;
bit *= 2;
}
Array.Reverse(dizi2);
uint deger = 0;
for (int i = 0; i <= dizi2.Length - 1; i++)
{
dizi2[i] *= sayicc[i];
if (sayicc[i] == 1)
{
deger += dizi2[i];
}
}
textBox2.Text = " " + deger.ToString();
}
else
{
MessageBox.Show("Deger girilecek alan boş.", "HATA!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
|
|
|
|
|
Why not this?
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox2.Text = Convert.ToInt32(textBox1.Text, 2).ToString();
}
else
{
MessageBox.Show("Deger girilecek alan boş.", "HATA!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
This code for binary to decimal. I didint see like this easy code. The other calculating codes are too long and complex. I made this and you can use easily.
|
|
|
|
|
Hello Peter,
your code is excelent , but have no control of what user enters, except if it is an emty string,
so I shall use your code with small change in that direction.
void Button1Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
try
{
textBox2.Text = Convert.ToInt32(textBox1.Text, 2).ToString();
}
catch (Exception)
{
MessageBox.Show("You didn't entered a Binary number !",
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You didn't entered a Binary number !",
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
|
|
|
|
|
Indeed your code is better for the real world. My point was about (badly) reinventing the string-to-number conversion.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
You know if I saw a piece of code that said binary to decimal, and if I said 1001, I'd expect to get 9 back instead of 1001! You would be in a world of hurt if I entered an 11 digit "binary" number and that is only slightly longer than 1/3 of a 32 bit int.
|
|
|
|
|
If you are saying what I think you are saying, then you need to take a close look at the second argument in the call to Convert.ToInt32() . It wouldn't be me that hurt.
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
True, didn't even notice you were using an overload I didn't know about.
|
|
|
|
|
Hello mehmetali,
peter have excelent solution ,
I made mine own which is not as elegant.
It is console application made in SharpDevelop C#.
This code is update of old one, it have no problem with entry of numbers as old version,
now it works with float point numbers and it folows full math logic.
using System;
namespace Binary_to_Decimal
{
class Program
{
public static void Main(string[] args)
{
string [] DecimalFloat = new string[2];
string Binary = "";
bool RealRelevance = false;
bool BinaryRelevance = false;
int Index = 0;
int Lenght = 0;
int Exponent = 0;
int Cipher = 0;
double BaseExponent = 0;
double RealNumber = 0;
double D = 0;
double F = 0;
string Negative = "";
string DecimalPart = "";
string FloatPart = "";
string Letter = "";
begining :
Console.Title = "Program Binary to Real ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Binary to Real");
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Enter binary number (exmpl: 1001) : ");
Binary = "";
Binary = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
RealRelevance = false;
RealNumber = 0;
RealRelevance = double.TryParse(Binary,out RealNumber);
if (RealRelevance == true)
{
if (Binary.Contains("-"))
{
Binary = Binary.TrimStart('-');
Negative = "- ";
RealNumber = -1 * RealNumber;
}
else if (!Binary.Contains("-") && !Binary.Contains("+"))
{
Negative = "";
}
else if (Binary.Contains("+"))
{
Binary = Binary.TrimStart('+');
Negative = "+ ";
RealNumber = double.Parse(Binary.ToString());
}
if ( Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
DecimalPart = DecimalPart.TrimStart('0');
if (DecimalPart =="")
{
DecimalPart = "0";
}
FloatPart = FloatPart.TrimEnd('0');
if (FloatPart =="")
{
FloatPart = "0";
}
if (DecimalPart =="0" && FloatPart =="0")
{
Binary = "0";
}
if (DecimalPart !="0" && FloatPart =="0")
{
Binary = DecimalPart;
}
if (DecimalPart =="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
if (DecimalPart !="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
}
else
{
Binary = Binary.TrimStart('0');
if (Binary =="")
{
Binary = "0";
}
}
}
else if (RealRelevance == false)
{
Console.WriteLine("Error , you didn't entered a number at all..");
Console.WriteLine();
goto end;
}
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for (Index=0;Index<Lenght ;Index = Index + 1)
{
Letter = Binary.Substring(Index,1);
if (Letter != "0" && Letter != "1" && Letter !=",")
{
BinaryRelevance = false;
Index = Lenght;
}
else
{
BinaryRelevance = true;
}
}
if (BinaryRelevance != true)
{
Console.WriteLine();
Console.WriteLine("Error , you didn't enter binary number ...");
Console.WriteLine();
goto end;
}
if (Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
D = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = DecimalPart.Substring(Index,1);
Cipher = int.Parse(Letter.ToString());
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
F = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = FloatPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Index+1;
Letter = FloatPart.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,-Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
F = F + (Cipher * BaseExponent);
}
RealNumber = D + F;
goto write;
}
else
{
Exponent = 0;
Cipher = 0;
RealNumber = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = Binary.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
RealNumber = RealNumber + (Cipher * BaseExponent);
}
}
write:
if (RealNumber == 0)
{
Negative ="";
}
Console.WriteLine();
Console.WriteLine("Binary number : " + Negative + Binary);
Console.WriteLine();
Console.WriteLine("Real number : " + Negative + RealNumber.ToString());
Console.WriteLine();
end:
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Do you want to run program again y / n : ");
Letter = Console.ReadLine();
if ( Letter == "Y" || Letter == "y")
{
goto begining;
}
}
}
}
Code was updated 09/02/2012.
Whole Solution for IDE SharpDevelop you can download from the bottom of this page :
Conversion from one to another number system [ Open in new window ]
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
modified 26-Feb-12 8:50am.
|
|
|
|
|
Peter's result would put "101" out as " 101" instead of " 5" Sorry, I'm wrong about that. didn't notice or know about the overload.
I still like the following logic a little better, it only allows int results, has more error checking, and gives immediate results as you type. (Change your event delegate)
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool isvalid = true;
string txt1="", txt2 = "HATA!!!";
string sayii = textBox1.Text;
int strt = 0;
int bitOffset = 1;
byte x;
if (sayii.Length == 0)
{
txt1 = "Deger girilecek alan boş.";
isvalid = false;
}
else if (sayii.Length > 32)
{
txt1 = "A binary integer can only be 32 bits long";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(0, 32);
return;
}
else if (sayii.Length == 32)
{
x = (byte)sayii[0];
if (x < 48 || x > 49)
{
txt1 = "A binary value must be 1 or 0";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(1, 31);
return;
}
if (x == 49) strt = int.MinValue;
sayii = sayii.Substring(1, 31);
}
for (int i = sayii.Length; i > 0 && isvalid; )
{
x = (byte)sayii[--i];
if (x == 49)
strt += bitOffset;
else if (x != 48)
{
txt1 = "A binary value must be 1 or 0";
isvalid = false;
}
bitOffset *= 2;
}
if (!isvalid)
{
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else textBox2.Text = " " + strt.ToString();
}
modified 10-Oct-11 21:09pm.
|
|
|
|
|
See this reply[^] above. The same comment applies here.
Also, have a look at this thread[^].
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Peter_in_2780 wrote: Also, have a look at this thread[^].
"this thread" got me here in the first place.
I know this is inefficient, but as a learning tool, this makes a good example of how signed integers work. (and maybe even let him learn how to code a little more efficiently.) I think his original example would say "1xyz1" was 33. (I don't remember his original code exactly, so I'm going from what I remember thinking of it.)
I actually learned something. My version has a bug in it. My "bit *= 2;" (my expectation and I forgot what I used for "bit") should have blown up when I tried to make it 2^32 power, instead it made it -2^31 (int.MinValue). To fix my bug, add "if (i > 0) " before that statement and take out the prior "if ...Length == 32" logic.
|
|
|
|
|
Are there any good reporting and database tools that you would recommend for C#?
Most likely would be using Visual C# Express for the IDE.
Something similar to the RAD tool in MS Access would be nice.
Thank you.
|
|
|
|
|
Stan Moong wrote: Are there any good reporting and database tools that you would recommend for C#?
I keep recommending Microsoft Access, as it's a great reporting-tool. It's kinda easy to put the data from Sql Server into an Access database, and it's damned easy to make a report in that database. Users can view the reports using the free runtime version[^].
It's my favorite reporting tool, as I can export the data that the report is based on along with the report (ideal when debugging), it's easy in maintenance, and well integrated in Office - doing a mailmerge from there shouldn't be a problem. An Access database can also contain custom code or forms, giving you a fast and flexible reporting-engine.
Have worked with Crystal (gave us headaches on updating), DevExpress (kinda nice, if you don't mind the DevExpress way of working) and the Report Designer that's in Visual Studio (showing them using the ReportViewer[^]). Each will have it's own merits; best would be to take a day to give them a try.
Bastard Programmer from Hell
|
|
|
|
|
Thank you for the information, Eddy. Will test them out.
|
|
|
|
|
I have seen many applications that make use of the ActiveSync connection to a mobile computer to perform functions. A couple of well known examples are remote registry editors and remote display tools. I'd like to understand the architecture better to know how to invoke a method on the desktop side that acts on the mobile computer side. For example, I'd like to write an application that detects when a device is up on ActiveSync, and then displays information about the mobile computer such as serial number, MAC address, manufacturer, model, etc. Many of these are P/Invoke calls, and I can write WinForm applications that get some of these variables that run on the device locally. I would like to better understand how to use ActiveSync to invoke such calls on a desktop WPF application, get the information from the device, and display it in a table within the WPF application. Would I make use of a socket of some kind? I don't know where to start with this.
Regards,
Scott
|
|
|
|
|
Hi
I am trying to build a calculator and use a WCF web service to do the math part. I need to pass the two integer arguments to my web service.
I defined the numbers on calculator as buttons.
Now I need to pass input to web service based on the two values clicked on calculator.
Can you please tell me how do I get the values into the button1 click event based on button4 and bitton 2. ( For now I just have two numbers on calculator..)
public partial class Calculator : System.Web.UI.Page
{
private int i, j;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MathClient client = new MathClient();
try
{
int ii = client.Add(5, 6);
Result.Text = Convert.ToString(ii);
}
catch (Exception ex)
{
Result.Text = ex.Message;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int i, j;
i = 4;
j = 4;
}
protected void Button2_Click(object sender, EventArgs e)
{
int i, j = 4;
}
}
|
|
|
|
|
When you declare the integer variables i and j inside the Button Click event of Buttons 3 and 2: you create are creating variables local to those event handlers: so your class-scoped integer variables with the same names are never getting set.
The Button1 Click event will, therefore, also see i and j as having their default value: 0.
So set i and j in the Buttons 3 and 2 event handlers, but do not declare them there.
best, Bill
"Use the word 'cybernetics,' Norbert, because nobody knows what it means. This will always put you at an advantage in arguments." Claude Shannon (Information Theory scientist): letter to Norbert Weiner of M.I.T., circa 1940
|
|
|
|
|