|
I may be wrong here, but I think when you type that in a DOS box, you're setting that variable only for that session. If you want to set permanent environment variables, go to Control Panel, System, Advanced, Environment Variables.
|
|
|
|
|
|
Hi,
this is how I understand it:
when a process starts, it gets a copy of the environment variables; it can add,
remove and modify them but that does not affect the outside world.
so when you open a command prompt, set a variable XYZ=123, run an exe, that exe will
see all variables including XYZ=123.
I havent done this, but I expect if you create a StartInfo, it then already copies
the current collection of environment variables, so you could add/remove/modify them
and only then start a process that would use that set.
Hope this helps.
|
|
|
|
|
Hello,
first many thanks to all for the answers, but my problem is another one.
So I want to try to explain my problem again.
And I hope someone can understand my problem.
I know my english is not so well.
I don't want to set,remove or modify an environment variable, I want just read all environment variables of a process.
So I wrote a program, which should identify all running processes on a maschine and then I want to get all environment variables of these seperate processes.
There are not my processes.
So I use for example : CurrentProcessID.StartInfo.EnvironmentVariables...
I get a collection with many environment variables but not the one I want to get.
I think I get all the variables which was defined in the system and the user environment but not in the process or session environment.
So I need a possibility to read all environment variables which are set during the process.
For example:
Someone starts a program on a maschine and this program sets some environment variables.
Now I want to read this special variable, only guilty for this process.
I hope I could explain my problem clearer.
Thanks
Stephan
|
|
|
|
|
Hi,
if I understand correctly, you want to know the current values for all envars in another
process ? Well AFAIK that is impossible: as I explained, a process starts of with a copy
of its parent process (see Process.StartInfo), but that copy is local data, it exists only
inside that process, and there is no handle or other way to get to it from the outside.
|
|
|
|
|
Hi,
this is what I meant. Correctly.
But there must be a way to get the information about the variables, because I know another programm, which get this informations.
Do you know the programm "ProcessExplorer". You can find this program on sysinternals.com.
Do you know another way ? For examples a WinApi-Function?
For example to get the parent id of a process I used the WinApi-Function "createToolhelp32Snapshot".
Maybe this is the correct way to get the information.
So I have to look.
If you know a function, please tell it.
Thanks
Stephan
|
|
|
|
|
Hi Stephan,
I tried SysInternals Process Explorer and as far as I can tell it shows the initial,
not the current, environment variables. So I continue to think seeing the actual values
is not supported nor possible on Win XP; only the process itself can read and write its
current values.
|
|
|
|
|
Hi Luc,
o.k. if you mean the process explorer shows only the initial environment variables, then you could explain me why the process explorer see more variables than my program.
Try this:
Start CMD
set TEST=ABC
notepad
Now search in process explorer the process "notepad".
Then doubleclick on it and look into the environments.
You can see the Variable TEST.
If I try to get this "TEST"-Variable with my program, I don't get this one with the methode Startinfo.EnvironmentVariables.
But I need this "TEST"-Variable.
Why ?
Stephan
|
|
|
|
|
Did you read my previous replies ?
Try this:
navigate explorer to the bin/debug folder of your C# program
Start CMD
do "dir" to make sure your exe is there
set TEST=ABC
nameOfMyCSharpProgram.exe
|
|
|
|
|
Hi Luc,
either I don't understand your replies, or you didn't understand my problem.
The programs which use the environment variables which I want to get are not my programs. So I don't know which environment variables they use. So I can't set this in a DOS-Box.
Another example:
You write a program. This program do nothing when it starts.
You write a methode which create a new environment variable, if a button is pushed. This environment variable is only guilty in your program, process, session.
Now you start your program but you don't push the button.
I start than the process explorer on the same maschine and search for your program/process.
The process explorer shows me all the environment variable which your program use.
I think these are the one, which stand in the user and system environment.
Then I start my special program.
This program should do the same as the process explorer.
My program shows me the same environmment variables which your program use.
Then you push the button. Your methode create a new environment variable.
I refresh the view of the process explorer. The process explorer shows me now one environment variable more. It shows me your new created variable.
I refresh the view of my program, but my program don't show me the new environment variable. Why?
I think that the dotnet framework cannot show me the variable, too.
I think I need a winapi-function.
For example to get the parent id of a process I used the createtoolhelp32snapshot-function. So I think I need a winapi function too.
Or did I do a mistake ?
My code snippet is the following.
private void button1_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Sorting = SortOrder.Ascending;
listView1.FullRowSelect = true;
listView1.Columns.Add("Name", 150, HorizontalAlignment.Left);
listView1.Columns.Add("Wert", 450, HorizontalAlignment.Left);
ListViewItem oItem;
listView1.Items.Clear();
Process[] localByName = Process.GetProcessesByName(textBox1.Text);
if (localByName.Length > 0)
{
Process oProcess = localByName[0];
foreach (DictionaryEntry de in _
oProcess.StartInfo.EnvironmentVariables)
{
oItem = listView1.Items.Add(de.Key.ToString());
oItem.SubItems.Add(de.Value.ToString());
}
}
}
To use the snippet you need on the form a listView, a pushbutton and a textbox-object.
If I haven't make an error in my code, do you have a code snippet who can read the environment variable, which is created in your example program ?
Thanks
Stephan
|
|
|
|
|
Hi again,
SteveA69 wrote: I refresh the view of the process explorer. The process explorer shows me now one environment variable more. It shows me your new created variable
AFAIK that is completely impossible; and I told you so days ago. No need to go on about this.
For every single process the outside world can only see the envvars that were in effect
when that process got started, everything else is hidden inside the process.
The "Process Explorer" you mentioned before does not do that either, probably for a good
reason: it cant be done.
|
|
|
|
|
Hi Luc,
I see you cannot help me in this case.
Thanks for your help and your answers.
Stephan
|
|
|
|
|
I am aware how to do it alphabetically (ascending/descending) but I am unfamiliar if its capable to do the same with numbers and such.
public static void DoSomething()
{
DoSomethingElse();
}
public static void DoSomethingElse()
{
Dosomething();
}
|
|
|
|
|
You'll likely want to write a custom IComparer for that. Then set the ListView s ListViewItemSorter property to an instance of your comparer. You do mean sort list items by column, not sort columns, right?
|
|
|
|
|
I will give it a try, thanks. Yes, that's what I meant.
public static void DoSomething()
{
DoSomethingElse();
}
public static void DoSomethingElse()
{
Dosomething();
}
|
|
|
|
|
Hello everyone,
I am thinking of using Backgroundworker to transfer data from a remote database into my Windows Apllication. In past I have worked with Backgroundworker to run some mathematical process. But in those case I always knew about the completion of the mathematical process and could check to see if it has been reached.
I am wondering how I would know the process in Data Transfer from a remote database has been completed?! Otherwise, how I am going to stop the backgroundworker?
Any ways, I was wondering if someone can guid me on this issue.
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
Khoramdin wrote: But in those case I always knew about the completion of the mathematical process
How did you know that the process has completed?
Khoramdin wrote: Otherwise, how I am going to stop the backgroundworker?
Like this:
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
resultLabel.Text = "Canceled";
}
else
{
resultLabel.Text = e.Result.ToString();
}
}
Look at this link to get details about BackGroundWorker.
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]
|
|
|
|
|
hello.
i am working on an irc client and now i've got two problems. i hope someone can help me ;D
i use asynchronous sockets.
1) the byte array i fill, is sooner or later filled completely. but i don't know how to clear the array and when i have to clear it, or do i even have to clear it? as soon as the array is full, the program throws - of course - an exception ("ArgumentOutOfRangeException").
2) sometimes i get the same input more than one time. for example: when i join a channel and save the nicknames to the nicklist, some nicknames appear twice or even more often. what do i have to do that this doesnt happen?
the code:
private byte[] data = new byte[4096];
public void InitSocket()
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Blocking = false;
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("194.124.229.58"), 6667);
client.BeginConnect(ipe, new AsyncCallback(OnConnect), client);
}
public void OnConnect(IAsyncResult ar)
{
Socket remote = (Socket)ar.AsyncState;
remote.EndConnect(ar);
remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), remote);
}
public void OnReceive(IAsyncResult ar)
{
Socket remote = (Socket)ar.AsyncState;
int received = remote.EndReceive(ar);
if (received == 0)
{
client.Close();
return;
}
string receivedtext = Encoding.Default.GetString(data, 0, received);
sw.Invoke(Addmsg, new string[] { receivedtext });
remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), remote);
}
if you have improvements on my code, please tell me ;D
kind regards
|
|
|
|
|
donjubs wrote: ) the byte array i fill, is sooner or later filled completely. but i don't know how to clear the array and when i have to clear it, or do i even have to clear it? as soon as the array is full, the program throws - of course - an exception ("ArgumentOutOfRangeException").
If you're re-using the array, you'll need to clear it or just re-create it. (To "clear" it, just set all its elements to 0, that's like resetting it, putting it back into it's created state). But the exception you're getting indicates you're trying to write more than 4096 bytes (e.g. data.Length) to the array. From the code you posted, I don't see that happening anywhere, which means there is probably some other code that tries to write beyond the end of the data array. On what piece of code is the exception occurring?
donjubs wrote: 2) sometimes i get the same input more than one time. for example: when i join a channel and save the nicknames to the nicklist, some nicknames appear twice or even more often. what do i have to do that this doesnt happen?
If you want only distinct names in your list, just do a check on the list for the name before adding it:
List<string> nicknames = new List<string>();
...
string newNick = "Johnny Sasaki";
if(!nicknames.Contains(newNick))
{
nicknames.Add(newNick);
}
|
|
|
|
|
1) the line is marked. see the comment in the OnReceive method. there is no other code (except for the OnSend method - but i don't thinkt that this one is the problem, cause it isn't used when the exception is thrown) that uses the array!
2) it was just an example. it may also happen with joins, quits etc. there has to be a way how to avoid that!?
|
|
|
|
|
1) What's sw? What's Addmsg?
2) Well, if you're using multiple threads to add and remove to the list of nicknames, you need a thread-safe structure or use a lock before reading and writing to the list. Have you considered looking at System.Collections.Generic.Dictionary<K, V>? It allows only unique keys. It will throw exceptions if you try to add duplicates. Like with any other structure, if you're using multiple threads to read and write to it, you'll need to do some locking to do this in a thread-safe way.
|
|
|
|
|
1) sw = form object
addmsg = delegate
guess there isn't the prob
2) yes, i've thought about it, but i don't get it working ;D well, guess i just have to try..
|
|
|
|
|
1) I bet the problem is in the Addmsg function. Can you post the code for that?
|
|
|
|
|
there is no function for that!
private delegate void Addmsgs(string msg);
private event Addmsgs Addmsg;
Addmsg = new Addmsgs(IRCStream);
IRCStream is a method, but it doesn't have anything to do with the array ;D
private void IRCStream(string line)
{
.
.
}
|
|
|
|
|
Yes, I meant the function that the Addmsg delegate is pointing to.
The exception may have been due to your passing a string[] to the Invoke method, which tries to call IRCStream(string), which doesn't take a string array (it only takes a string).
By the way, what does the exception message say? And what does the exception stack trace look like?
|
|
|
|
|