|
Hi,
I'm a beginner Windows programmer. I need to write a program that will copy files across networked servers, which may need to authenticate the user as he/she access the remote server. The user may type "\\remoteserver\d$\afile". If the user has accounts on both servers, it will just copy. If the currently logged in user does not have the same account on the remote server, the program is supposed to get the user id and password and pass it along to the remote server, just like the Windows Explorer.
I went round and round and trying with WNet APIs, System.Security.Permissions, System.Security.Principal. I did not get anywhere.
Any help will be greatly appreciated.
Thanks!!
|
|
|
|
|
Actually - in a domain setup - the user account doesn't exist on different computers (except, perhaps, different DCs that either cache or backup accounts). That's only a low-grade workgroup trick or for local users.
To accomplish what you want, you have to program the prompt yourself and deal with impersonation (which may cause problems on the local end if the impersonated user doesn't have access to the source resource). The fact is that .NET is a basic framework and doesn't include all functionality in Windows.
First, attempt a File.Copy . If you get an UnauthorizedAccessException , show a modal dialog to get the username and password (of course, mask the password using the TextBox.PasswordChar property). Then you're going to want to impersonate the requested user account. See the SDK documentation for WindowsIdentity.Impersonate for details and very good example source (P/Invoking native functions is required in .NET 1.x at least). Finally, perform the File.Copy again, and undo and close the impersonation handles (this is covered in the sample source I mentioned).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath, Thank you so much for your help. I will give it a try.
~Sherry
|
|
|
|
|
Im am trying to write an application that involves alot of TextBoxes that the user can input to. Is there a way to create an array of TextBoxes so I can just loop through them all?
Thanks
Matt
|
|
|
|
|
Yes. The easiest (if not the most type-safe) way is to create an ArrayList and Add(aTextBox) for each TextBox that is created.
Then when you need to loop over them you can do
foreach(object obj in myArrayOfTextBoxes)
{
TextBox tb = (TextBox)obj;
}
I'd recommend creating a type-safe collection to hold your text boxes in though.
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
|
|
|
|
|
TextBox[] txt = new TextBox[10];
txt[0] = txtForm1;
txt[1] = txtForm2;
....
Sorry, no short way that I know of.
"if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.
Support Bone
|
|
|
|
|
TextBox[] txt = new TextBox[] {txtForm1, txtForm2};
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Let me start by saying I am very new to multithreading.
I have a web app where I want to execute two searches in parallel, when the both finish, move on and process the returned data. I did what I thought would accomplish this but my search times did not change. What am I missing?
Also, if it matters, both search functions are adding nodes to the same xml document in memory. Each function has duplicate checking before it enters the data. Could I be getting caught by resource locking to keep from really multithreading? thoughts?
Here is a piece of what I have so far. Thanks in advance.
<br />
Thread seaThread = new Thread(new ThreadStart(GetSeaAttachments)); <br />
Thread ilThread = new Thread(new ThreadStart(GetIlAttachements)); <br />
<br />
seaThread.Start(); <br />
ilThread.Start(); <br />
<br />
seaThread.Join();<br />
ilThread.Join();<br />
<br />
if (seaThread.IsAlive)<br />
{<br />
seaThread.Abort();<br />
}<br />
if (ilThread.IsAlive)<br />
{<br />
ilThread.Abort();<br />
}<br />
|
|
|
|
|
The reason you're not noticing any differences in time - and why you don't currently have to worry about resource locking - is in the following code you wrote:
seaThread.Join();
ilThread.Join(); The method Thread.Join blocks the current thread (your page's thread of execution) till the thread is done. So in your code, the page thread waits till the seaThread is finished, then executes the ilThread and waits till it finishes. This also means that your checks to see if the thread is still alive are pointless. The thread is alive until it either completes or is aborted. Since you're joining thread, the thread will be allowed to complete unless the thread itself (or another thread with a reference to said thread) aborts itself.
As far as resource locking is concerned, resources are synchronized unless otherwise stated. Some resources (like various collects and lists) have a SyncRoot (as it's commonly called), which returns a static object that's been initialized. With this, you can (and must in this case) perform locking yourself. In C#, you have the handy lock keyword. You don't have to use the SyncRoot for classes that have it, though - this is simply for disparate threads in different blocks of code (so you don't have to worry about passing your own object to synchronized against).
To use your own sync objects, one typically locks against typeof(MyType) for static methods and properties that need to be synchronized, and locks against this for instance methods and properties. See the lock keyword documentation for more information and examples.
So, to add resource locking to your XmlDocument (which doesn't have the SyncRoot property I was talking about), your threads need to lock against something (like either this , which refers to the page, or to the instance of your XmlDocument (make sure you've instantiated it first, though, since locking against null throws an exception). For example, lets say that both threads want to write something to the document. In either thread, do something like this:
XmlElement elem = this.myXmlDocument.CreateElement("exmaple");
lock(this.myXmlDocument)
{
this.myXmlDocument.DocumentRoot.ChildNodes.Add(elem);
} Notice how I created the element before locking? Knowing when to lock your resources is important. Locking them too early could result in O(n) execution times, instead of O(n/m) (where m is the number of threads) like you desire. And you typically don't need to worry about locking while reading a resource, unless you face a potential reader/writer problem. In that case, see the ReaderWriterLock in the System.Threading namespace.
Finally, to suggest a solution to your problem, see the documentation for the ThreadPool class. Besides the benefits gained from a thread pool (like you don't have too many threads spawning uncontrollably), you have many other gains from methods like ThreadPool.RegisterWaitForSingleObject , which lets you use a WaitHandle you want to create (such as an AutoResetEvent handle or a Mutex ) which gives you signaling (helpful when synchronizing threads) capabilities, plus it allows you to pass state information to the thread that will be spawned (where the Thread class forces you to use fields to store data, which isn't thread safe without going to a lot of work to synchronize).
Basically, you'd be wise to read the SDK documentation for the System.Threading namespace. It may be daunting at first, but - when yielded properly - can be very powerful.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for all the information But I am still a little confused. The seaThread.join line stops the current thread from processing till the seaThread finishes. That is what I want and since I have already started the ilThread, shouldn't it continue to run. the ilThread.join will hold the main thread in case it takes longer. I do want the main processing to stop till both threads finish.
I have looked into the threadpool a bit but find it just as confusing. I don't think my task really that complicated. How will thread pooling help me there?
For simplicity sake, I tried this with just a console program and it is working as I would expect. It is below.
<br />
using System;<br />
using System.Threading;<br />
<br />
namespace test_multithreading<br />
{<br />
class Class1<br />
{<br />
public static void PrintCurDateTime()<br />
{ <br />
Console.WriteLine( " Secondary Thread Started" );<br />
for (int i = 1; i <= 10; i++)<br />
{<br />
Console.WriteLine(" Second Thread working");<br />
Thread.Sleep(1985);<br />
}<br />
Console.WriteLine( " Second Thread time: " + DateTime.Now.ToLongTimeString() );<br />
Console.WriteLine( " Second Thread Finished" );<br />
} <br />
public static void PrintFutureDateTime()<br />
{ <br />
Console.WriteLine( " Third Thread Started" );<br />
for (int i = 1; i <= 100; i++)<br />
{<br />
Console.WriteLine(" Third Thread working");<br />
Thread.Sleep(93);<br />
}<br />
Console.WriteLine( " Third Thread time: " + DateTime.Now.ToLongTimeString() );<br />
Console.WriteLine( " Third Thread Finished" );<br />
} <br />
public static int Main(String[] args)<br />
{<br />
Console.WriteLine("Main Thread \n");<br />
<br />
try<br />
{<br />
Thread futThread = new Thread(new ThreadStart( PrintFutureDateTime ));<br />
futThread.Priority = ThreadPriority.Highest;<br />
futThread.Start();<br />
<br />
Thread secThread = new Thread(new ThreadStart( PrintCurDateTime ));<br />
secThread.Priority = ThreadPriority.Highest;<br />
secThread.Start();<br />
<br />
Console.WriteLine("Waiting for theads to join");<br />
secThread.Join();<br />
futThread.Join();<br />
Console.WriteLine("Joined");<br />
<br />
Console.WriteLine( "Main Thread time: " + DateTime.Now.ToLongTimeString() );<br />
<br />
}<br />
catch (Exception e) <br />
{<br />
Console.WriteLine( e.ToString());<br />
}<br />
return 0;<br />
} <br />
}<br />
}<br />
|
|
|
|
|
Hi,
I want to know how can I play audio files like mp3 files with basic functionality of play/pause/stop/forward/rewind.
Thanks.
Paul
|
|
|
|
|
You could try searching first. Use that little textbox torward the top of this page labeled "Search:", or click "Search Comments" to search this forum because that topic has been covered to death. There are plenty of examples on CodeProject, and many more if you google the 'net.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi
We have created a small application with a RichTextBox on. In the RichTextBox we can write text and add pictures by copy-pasting them. Later on we can press a button and save the contents into a table on a SQL Server 2000*.
This works fine, the problem is that the size of the added pictures is huge. Adding just a screenshot of the app. to the textbox adds another 3 MB to the size of the DB.
Does any of you have an idea on how to decrease the size of the pictures?
Currently saving/loading data works with the use of filestreams. When we press the save button, the contents of the textbox is written to a file - and then the file is saved in the DB as a BLOB.
Thanks in advance, Mads
*) Of course, we can also load the contents back into the app.
|
|
|
|
|
The image is what it is. If the image is 3MB, it's going to be that large. About your only options are to compress the image (either using image compression or stream compression (like ZIP)) and storing that blob in SQL Server. Unless the size of the database (which is typically on a server that can handle it) is really an issue, the above options will hamper performance. Disk size (and memory, for that matter) is cheap - CPU cycles are not.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
We had the same issue. Any OLE object attached to the RTF is stored as text and bloats the size of RTF. We implemented compression before sending the data to the server. This reduces the time to transfer data, but does add a some amount of time on the client (which may be negligitable).
--
Joel Lucsy
|
|
|
|
|
Neither ADO or ADO.NET has this capability. You can, instead, use an interop assembly with the Jet DDL to create the MDB and then access it using the ADO.NET classes. See the following article for more information: http://www.tek-tips.com/gfaqs.cfm/pid/796/fid/3888[^].
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Does anyone know about creating a new database by coding with ADO.NET?
for example access(*.mdb)
in this example code must create mdb file and all tables and file needed for database .and i want create new tables in database and update database.
Regards'
Amir Jalaly
The life is two days ,
one day is paid to love someone ,
another day is paid to hate him ,
|
|
|
|
|
First of all, excuse my english. I hope you understand what I am searching for here.
I want to display text in a RichtextBox/TextBox ( size of A4 ), the same way as MS Word or a PrintPreView does ( eg. a true WYSIWYG display and output). Is that possible?
Is there a way to convert the font used into a "printsized Font/fontFamily" using Graphics/Font?
Many thanks in advance.
// Loke - n00b at the moment. =)
|
|
|
|
|
Hello!
I'm coding a Wizard in C# and on page 1 in my wizard you can, by a ComboBox, choose a language for the wizard. I don't want to change language by rightclicking on the project in solution explorer, selecting properties, configuration properties, Debugging and then write for example "de-de" in the Command Line Argument.
This is a little part of my code for page 1:
switch(comboBox1.Text)
{
case "German":
{
CultureInfo myCIintl = new CultureInfo( "de-de", false );
Thread.CurrentThread.CurrentCulture = myCIintl;
Thread.CurrentThread.CurrentUICulture = myCIintl;
}
case .........
I know I have to kill the page in one way to use ResumeLayout() but I don't know how. Some kind of refresh must happen anyway for all my buttons, labels etc in all pages so they will change language when I have chosen from the ComboBox.
I hope I have explained enough for anyone to understand... Please help me!!!
/Anna
|
|
|
|
|
Simply refreshing (ala Control.Refresh ) or resuming the layout (ala Control.ResumeLayout ) won't do it. Refresh merely invalidates the control's region and forces a repaint. ResumeLayout is only used during initialization of the control to make sure that no layout code is executed (as long as the control implements it right) until the method is called. This is many times necessary and often times save CPU cycles.
Switching the CurrentUICulture is only the first step. Normally, you do this in a program before calling Application.Run in your app's entry point (static Main ). This is also done automatically on non-English Windows OSes or Windows with an MUI pack installed and enabled for the current user. This ensures that the main thread (the thread on which all UI operations should be / need to be done) - and any threads that it spawns - use the culture for both regional information (dates, times, numbers, etc.) and for the UI.
The problem with doing this after the UI thread is executing is that you need to re-initial all relevent values. Basically, you need to do everything found in InitializeComponents (if using the VS.NET form/control designers) except instantiating the control and adding it to its would-be parent's Controls collection. Any properties that should be localized (like Text , Size , Location , etc.) in your app need to be reassigned. Having set the CurrentUICulture prior to this act will ensure that the right localized values are obtained (if available, otherwise the primary assembly's resources are used).
Also, to avoid unnecessary look-ups for the language in which the app and its libraries were written (for example, everything I develop is inherently in en-US), you should use the NeutralResourcesLanguageAttribute (an assembly-level attribute) that tells the ResourceManager (et. al.) that it can find the specified culture's resources in the primary assembly (rather than performing look-ups for an appropriate satellite assembly, which can very expensive when touchless deployment over the Internet).
I know it doesn't sound easy, but its necessary. The docs affirm as much. One thing that would make it slightly easier - but it will break the ability to use the designer (personally, I don't like the designer after initial layout, so I could care less) - is to separate the initialization code from the property assignment code, so that you instantiate and add controls to their parents' Controls collections in one method, and assign all the property values in another. You should still call SuspendLayout and ResumeLayout , as well as BeginInit and EndInit for types implementing ISupportInitialize (again, the form/control designers will do this automatically), when appropriate in the method that instantiates the controls (after instantiation of course). Call the beginning methods before calling the property-assignment method, and then call the ending methods after the method returns.
Then, when the culture is changed, you need only call the property-assignment method again. The controls are already initialized and any changes to their Size or Location would also be reflected.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi,
I have a datagrid with several columns, one is "Finish Date", I would like that the rows that has a finish date greater than the current data have a different background color that the other one.
I have thought about create a calculated, hidden column, that will hold the column of the row, it's possible then bind that calculated column, to the background color of the row ?, Is there another easier way to do it ?
thanks in advance, greetings
Braulio
|
|
|
|
|
The unfortunate truth is that in ASP.NET, doing this with a DataGrid (System.Web.UI.WebControls.DataGrid , that is) is incredibly easier. You handle the ItemDataBound event, which gives you the item (the object being bound) to which you can set properties based on the data being bound, such as different font styles, colors, etc. Easy, huh?
I'm guessing you're not doing this in ASP.NET, though. Now it gets harder. The best way is to derive your own class from DataGridColumnStyle (or perhaps even form DataGridTextBoxColumn which you're most likely already using for the column style). If you choose the latter, it already exposes the TextBox that is hosted when you edit the cell. Note, though, that it is not enough to set the BackColor of the TextBox because it's only visible when the cell is being edited.
So, after deciding which you want to do (I recommend the latter), override the Paint method and - based on the value bound to the cell - paint the appropriate background.
The only thing left to do is modify the DataGrid initialization code. If you're not already using a DataGridTableStyle , you should because it gives you a high degree of control over the display and is still versitile enough to handle different data sources. See the DataGrid.TableStyles property for more info.
To this DataGridTableStyle , you add your various DataGridColumnStyle derivatives, such as the DataGridTextBoxColumn , the DataGridCheckBoxColumn , and your new-fangled derivative you created from above. If you already have one, just modified the source for that column to use your object instead of the DataGridTextBoxColumn instance.
It may sound like quite a bit of work, but this is an extremely versitile solution. Instead of handling special cases that require code every time you run into this problem, you make a single class that you can easily reuse - which is what component development is all about! If you create your own little library of handy classes like this, you won't even have to copy and paste source files from project to project - just reference the library assembly and specify the type. In the end, it definitely pays off and is "in the spirit" of the .NET base class library, i.e. extending it rather than working around it.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for your answer. I'm using windows forms, well a bit of work to make that, it's a pity that the VBA get effects like this so fast, and we have to spend some time into it ( how can you explain to that guy that the first time you need an hour to make a thing that he makes in five minutes ).
Another problem that I found on the Windows Form Datagrid, is that there not an easy way to fake the load of a result ( I mean, load only the first 1000 entries, then by demand load the others or all, something like that).
Greetings
Braulio
|
|
|
|
|
Hello everyone,
these days im thinking of building a chat server of my own with a chat application ofcourse too...
but im thinking about which technology i should use...
for me i already have knowledge in c++ and vb.net, but i believe that c++ is faster and i think c# is faster also from vb.net.
VB.net can do it, but again i think c# is faster...
so which language do u advice me to do it on, c++ or c# or vb.net, or even c++.net? and why? and which is faster? and is there articels that are helpfull that i cant read.
very thankfulllllll
|
|
|
|
|
Hello everyone,
these days im thinking of building a chat server of my own with a chat application ofcourse too...
but im thinking about which technology i should use...
for me i already have knowledge in c++ and vb.net, but i believe that c++ is faster and i think c# is faster also from vb.net.
VB.net can do it, but again i think c# is faster...
so which language do u advice me to do it on, c++ or c# or vb.net, or even c++.net? and why? and which is faster? and is there articels that are helpfull that i cant read.
very thankfulllllll
|
|
|
|
|