|
This better way of getting 1.
value += (int)(Math.Pow(Math.Sin(3.2), 2) + Math.Pow(Math.Cos(3.2), 2) + (8 / Math.Floor(8.7)) - 1);
|
|
|
|
|
Hi,
I've a university issued laptop with windows 7 on it. When I click on "windows" button it show my name as one of the system folder, but there is no path for it. It is above the "My Document" folder, in fact everyone in the university has same folder with their name [obviously]. I want to make an program that could extract the name of this folder when its it run on a particular system. And computer name, account name or name of Environment.getFolderName(SpecialFolder.Personal) all are different from this folder. I do not know where is this folder even stored.
CHECK THIS LINK FOR AN IMAGE
http://dl.dropbox.com/u/10120092/Untitled.png[^]
SHIVAM
|
|
|
|
|
There isn't a direct way, this however should give what you want:
string s=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
s=s.SubString(0, s.Length-8);
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Shivam (/Luc);
You can access this folder with the SpecialFolder UserProfile ;
String s = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
|
|
|
Only if using .NET framework V4
|
|
|
|
|
Well the OP did not make reference to any .Net version, i don't know what he is using
[at least he knows now it exists in later frameworks!)
|
|
|
|
|
True.
I've had a look in reflector and it's easy enough to do this in previous versions - see my other answer[^] below
|
|
|
|
|
Hi, nothing seems to work. I tried your way, it only shows some other folder on my desktop.
|
|
|
|
|
try to be specific. What have you tried and what was the outcome?
What operating system, which .NET version, what exactly do you get, what exactly would you like to get.
It works for me!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I tried your code.
I get "C:\Users\100xxxxxx\Desktop" where 100xxxxxx is my student ID.
I'm using .Net 3.5 but I do have .Net 4 with visual studios 10. Not why it is not working for me..
S
|
|
|
|
|
And is there a folder somewhere that holds your actual name instead of your student ID?
All GetFolderPath() results are actual folder names.
When a user account gets created, the user name displayed is also used as the folder name; it is however possible to change one without changing the other, as explained e.g. here[^]. I guess that is what happened to the student accounts: first create them numerically, then modify their display names. The display name of the current user should be available through Environment.UserName
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Very strange but I do not see this folder anywhere on my computer. It shows when I press windows button or when I'm choosing or opening some file. I tried Environment.UserName but it is showing my student ID [LOL]. Anyways thanks, I checked your link, probably this what happening in my case too. DAM IT, but there has to be some folder which starts with my name.
Shivam
|
|
|
|
|
You're welcome.
BTW: Most of the username/foldername magic is hidden in the registry, as the article explains.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
YUPPII. I solved the problem. I searched my name inside the registry and found that everyone is school has same registry key to that their name as registry value..so in my program I'm reading the registry value from this registry key..and it is working fine..thanks everyone again..
SHIVAM
|
|
|
|
|
As an alternative to Luc's suggestion you can combine the SystemDrive and HOMEPATH environment variables something like this:
public static string GetUserFolder()
{
return
Environment.GetEnvironmentVariable("SystemDrive") +
Environment.GetEnvironmentVariable("HOMEPATH");
}
Edit: Just tested from explorer and using Process.Start and it appears to work with only the HOMEPATH variable too
|
|
|
|
|
Having seen Dave's answer that it is available in v4, I've had a look at how it's done using reflector and these two functions combined perform the same task and will work with earlier .NET versions so long as it's run on a platform that supports it - if not, a PlatformNotSupportedException will be raised.
public const int UserProfile = 40;
[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
public static string GetUserPath()
{
StringBuilder resultBuilder = new StringBuilder(260);
int num = SHGetFolderPath(IntPtr.Zero, UserProfile, IntPtr.Zero, 0, resultBuilder);
if (num == -2146233031)
throw new PlatformNotSupportedException();
return resultBuilder.ToString();
}
|
|
|
|
|
I have been using C# and Visual Studio for about 6 months now and it has worked fine. Recently, I don't know what I did, but when I try to publish a program now it doesn't work properly. I have no build errors, but when I click on publish under the menu (or in the project window), it says "Could not publish because a project failed to build."
I searched for this online and found it can sometimes be caused by DevExpress. So, I uninstalled DevExpress, and I still get the problem.
I thought I might have done something clumsy in code, so I started a completely new solution, a windows form, and clicked publish. I still get the problem.
One thing I noticed is when I click publish, a "(name of project)TemporaryKey.pfx" appears in the project window, at about the same time the error happens. Could that maybe have something to do with my problem? (I know very little about keys.) Any ideas? Thanks for any help.
|
|
|
|
|
Just an idea. Maybe the key is automatically generated on the first publish. If that is the case, it may have an expiration date, say, one year after the initial build. If it is expired, perhaps that is what is causing the build to fail. Maybe try generating a new one?
Though, the fact that you had the same problem when creating a new project suggests that the problem is something else. I imagine you'd get a new temporary key when building a new project (assuming it is in a new solution).
|
|
|
|
|
Hello i have this problem.
I need to send a message from a child window to a parent window using sendmessage but i can't make it work to send the Size object to my lParam parameter and i don't know how to cast it when catching it in the parent window so i get the valid Size object i send can someone tell me how?
i am getting Cannot convert type 'System.Drawing.Size' to 'int' when calling sendmessage i don't know how to pass it there, thanks
|
|
|
|
|
manchukuo wrote: Cannot convert type 'System.Drawing.Size' to 'int'
Why have you declared lParam as an int. This will possibly fail on a 64Bit system as it expects an IntPtr which is 8 bytes on X64 and 4 bytes on X86.
You could declare the SendMessage function in this case with lParam as ref Size .
|
|
|
|
|
ok i change from this
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd,
int Msg,
int wParam,
int lParam);
to this
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd,
int Msg,
IntPtr wParam,
IntPtr lParam);
but now i am getting errors in wparam where i am passing a int, how can i pass the int now and about the part to declare as ref Size i can't because i might use another type in there in the future
|
|
|
|
|
You should NOT pass an int directly as I explained - an int is 4 bytes but on a 64 bit system the function requires 8 bytes.
If you really need to pass a value directly you should create an IntPtr from it:
new IntPtr(yourInt)
By the way, the return value should also be an IntPtr.
Don't forget you can overload the method, so long as the size of the parameters are correct it won't fail.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
internal static class NativeMethods
{
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(
IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(
IntPtr hWnd, int Msg, IntPtr wParam, ref Size lParam);
}
Now you need to work out how to get the size back out once you receive the message, and you may need to pin the Size parameter in memory so it doesn't get moved or go out of scope and get collected...
|
|
|
|
|
ok it worked like a charm thank you soo much ,
just a question, isn't there a general way so it could only be one declaration and be able to pass any object?
modified on Friday, May 6, 2011 2:44 PM
|
|
|
|
|
That is the purpose of an IntPtr. When passing data using unmanaged memory a portion of that memory should be allocated to holding the data. The pointer to that memory location is then passed to the function. On the receiving side of the message the pointer is retrieved and the data read at that location then the memory freed. There are several ways to do this, the easiest is a combination of the BitConverter and Marshal classes.
Something like this will convert your Size to/from an IntPtr:
public static IntPtr SizeToPointer(Size size)
{
byte[] bytes = new byte[8];
Array.Copy(BitConverter.GetBytes(size.Width), 0, bytes, 0, 4);
Array.Copy(BitConverter.GetBytes(size.Height), 0, bytes, 4, 4);
IntPtr result = Marshal.AllocHGlobal(8);
Marshal.Copy(bytes, 0, result, 8);
return result;
}
public static Size PointerToSize(IntPtr pointer)
{
byte[] bytes = new byte[8];
Marshal.Copy(pointer, bytes, 0, 8);
Size result = new Size(BitConverter.ToInt32(bytes, 0), BitConverter.ToInt32(bytes, 4));
Marshal.FreeHGlobal(pointer);
return result;
}
It is very important that the memory is freed! I have done this in the PointerToSize function. If this is not called then the memory will leak.
[Edit]
I have just tested this with two forms. With both forms shown, this in form1:
NativeMethods.SendMessage(
form2.Handle, 100, IntPtr.Zero, NativeMethods.SizeToPointer(
new Size(123, 456)));
and this in form2
protected override void WndProc(ref Message m)
{
if (m.Msg == 100)
{
Size size = NativeMethods.PointerToSize(m.LParam);
MessageBox.Show(size.ToString());
}
base.WndProc(ref m);
}
The 100 was just a random number I chose for the message - it will be important to use numbers that Windows doesn't already use!
[/Edit]
[Edit2]
Check this[^] out to make sure you don't clash your message numbers.
[/Edit2]
Dave
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
modified on Friday, May 6, 2011 3:34 PM
|
|
|
|
|
If you're not happy with all the conversions to/from byte[] then it can be done instead with a class decorated with the StructLayoutAttribute. You can wrap the size in a custom wrapper class and use two other methods of the Marshal class which makes life a bit simpler. Each field in the class must be a blittable[^] type.
public static IntPtr ConvertSizeToPointer(Size size)
{
SizeWrapper wrapper = new SizeWrapper(size);
IntPtr result = Marshal.AllocHGlobal(Marshal.SizeOf(wrapper));
Marshal.StructureToPtr(wrapper, result, true);
return result;
}
public static Size ConvertPointerToSize(IntPtr pointer)
{
SizeWrapper sizeWrapper = new SizeWrapper();
Marshal.PtrToStructure(pointer, sizeWrapper);
Marshal.FreeHGlobal(pointer);
return sizeWrapper.Size;
}
[StructLayout(LayoutKind.Sequential)]
internal class SizeWrapper
{
private int height;
private int width;
public SizeWrapper()
{ }
public SizeWrapper(Size size)
{
this.width = size.Width;
this.height = size.Height;
}
public int Height
{
get { return height; }
}
public Size Size
{
get { return new Size(width, height); }
}
public int Width
{
get { return width; }
}
}
|
|
|
|
|