|
I have a requirement where when a button is clicked the app connects to a remote pc and the user can browse to a folder on the c drive then copy a file to their pc (not on a LAN but a remote location)
When using Remote desktop connection the details are (for example)
Computer: abcd.dyndns.org:1234
Username: bob2\apple
Password: password
After doing a bit of research using WMI or Impersonation seems the best options. Here's where I'm at with the Impersonation approach..
[DllImport("advapi32.DLL", SetLastError=true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
private void button4_Click(object sender, EventArgs e)
{
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsImpersonationContext wic = null;
try
{
IntPtr admin_token = new IntPtr();
if (LogonUser("bob2\apple", "abcd.dyndns.org:1234","password",9, 0, ref admin_token) != 0)
{
wic = new WindowsIdentity(admin_token).Impersonate();
File.Copy(@"", @"",
true);
MessageBox.Show("Copy Succeeded");
}
else
{
MessageBox.Show("Copy Failed");
}
}
catch(Exception se)
{
int ret = Marshal.GetLastWin32Error();
MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
MessageBox.Show(se.Message);
}
finally
{
if (wic != null)
wic.Undo();
}
}
thanks
Mat
|
|
|
|
|
Do you need file access to take place in the context of the actual user who is on the web site? Or, is this a situation where access to the web site means access to the files? If you need the Windows admins to control access to these files, then yes you need to use impersonation, but if you don't need that, it would be much easier to just stream the file back over HTTP. In that case file access would be done by the ASP_NET User, so you would only need to grant one user access to the folder. In the impersonation case, you have to add users to groups and whatnot to manage access to that folder.
Streaming a file back is super easy...
var file = File.Open(path);
Response.Clear();
Response.ContentType = "application\xml";
Response.WriteFile(file);
Response.Flush();
Response.Close();
Response.End();
|
|
|
|
|
matleeds wrote: I have a requirement where when a button is clicked the app connects to a remote pc and the user can browse to a folder on the c drive then copy a file to their pc (not on a LAN but a remote location)
Then hopefully there is also another requirement/assumption that states that the user must have remote access privileges on that computer sufficient to allow that.
|
|
|
|
|
please help me i want to create password for my win form and how can i manag it
|
|
|
|
|
The same way you create any form. Add a label and a text box that has the PasswordChar or UseSystemPasswordChar property. Then use ShowDialog to display the form at the beginning of the application.
Use the best guess
|
|
|
|
|
I think the answer you may be looking for is in the region of encryption and decryption.
Basically it boils down to never storing a clear text password - use something similar to the crypto class described here[^].
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
for examle:
I want to start IE with process.start method
and i want to get the text of address bar Is there any chance to do it
|
|
|
|
|
|
Thank u very much ,I don't know the key words for serching now I know
|
|
|
|
|
Member 10016142 wrote: I don't know the key words for serching That's part of the wonder of Microsoft.
Use the best guess
|
|
|
|
|
Hi all,
I'm creating an application that will allow me to draw lines with the mouse and then save them to a file in a graphical format. By dint of trying and trying I have come to the code.
The last problem that I can not solve is that I can only save the 'background' of the image but not the drawed shape.
The program consists of a PictureBox1 and a button1, drawing with the left mouse button, closes the shape with the right button and save the.
Thanks in advance to those who want to give me a tip.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap bm;
Graphics g;
GraphicsPath path = new GraphicsPath();
Pen redPen;
Pen blackPen1;
Pen blackPen2;
Pen orangePen;
public Form1()
{
InitializeComponent();
g = pictureBox1.CreateGraphics();
redPen = new Pen(Color.Red, 2);
blackPen1 = new Pen(Color.Black, 1);
blackPen2 = new Pen(Color.Black, 2);
orangePen = new Pen(Color.Orange, 1);
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
Shown += Form1_Shown;
}
private void Form1_Shown(Object sender, EventArgs e)
{
}
private List<Point> polygonPoints = new List<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
polygonPoints.Add(new Point(e.X, e.Y));
if (polygonPoints.Count > 1)
{
path.AddLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]);
g.DrawPath(redPen, path);
}
break;
case MouseButtons.Right:
if (polygonPoints.Count > 2)
{
path.CloseAllFigures();
g.DrawPath(redPen, path);
g.FillPath(new SolidBrush(Color.Orange), path);
}
break;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = Graphics.FromImage(bm);
for (int i = 0; i < pictureBox1.Width; i += 10) g.DrawLine(blackPen1, i, pictureBox1.Height, i, 0);
for (int i = 0; i < pictureBox1.Height; i += 10) g.DrawLine(blackPen1, 0, i, pictureBox1.Width, i);
g.DrawLine(blackPen2, 0, pictureBox1.Height - 90, pictureBox1.Width, pictureBox1.Height - 90);
e.Graphics.DrawImageUnscaled(bm, 0, 0);
}
private void button1_Click(object sender, EventArgs e)
{
bm.Save("c:\\test.png", ImageFormat.Png);
}
}
}
|
|
|
|
|
Member 7921268 wrote: Thanks in advance to those who want to give me a tip.
Declared on top of the code;
Graphics g; A bit confusing, as you're also painting on "g" in the "_Paint" method. Are you drawing on the correct graphics-object? The reason I ask, is because there's two of them;
g = pictureBox1.CreateGraphics();
Graphics g = Graphics.FromImage(bm);
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You are right!, I'll investigate about it.
Thanks
|
|
|
|
|
If you want to save the drawing for later edits, it is usually better to save the points to a file, and not the image. Its more of a suggestion than anser but...
|
|
|
|
|
Thanks for your reply but I need to save the shape in image format for a successive 'shape matching' process.
Regards.
Lenny
|
|
|
|
|
Member 7921268 wrote: Thanks in advance to those who want to give me a tip.
Solved!, thanks all
Working release:
using System.Collections.Generic;
using System.Text;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool flgDraw = false;
Pen redPen;
Pen blackPen1;
Pen blackPen2;
Pen orangePen;
Bitmap drawing;
GraphicsPath path = new GraphicsPath();
private List<Point> polygonPoints = new List<Point>();
public Form1()
{
InitializeComponent();
redPen = new Pen(Color.Red, 2);
blackPen1 = new Pen(Color.Black, 1);
blackPen2 = new Pen(Color.Black, 2);
orangePen = new Pen(Color.Orange, 1);
panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
Shown += Form1_Shown;
drawing = new Bitmap(panel1.Width, panel1.Height, panel1.CreateGraphics());
Graphics.FromImage(drawing).Clear(Color.White);
}
private void Form1_Shown(Object sender, EventArgs e)
{
Grid();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (flgDraw)
{
Graphics Gpanel = Graphics.FromImage(drawing);
panel1.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
flgDraw = true;
Graphics Gpanel = Graphics.FromImage(drawing);
switch (e.Button)
{
case MouseButtons.Left:
polygonPoints.Add(new Point(e.X, e.Y));
if (polygonPoints.Count > 1)
{
path.AddLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]);
Gpanel.DrawPath(redPen, path);
}
break;
case MouseButtons.Right:
if (polygonPoints.Count > 2)
{
path.CloseAllFigures();
Gpanel.DrawPath(redPen, path);
Gpanel.FillPath(new SolidBrush(Color.Orange), path);
}
break;
}
panel1.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
flgDraw = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(drawing, new Point(0, 0));
}
private void Grid()
{
Graphics Gpanel = Graphics.FromImage(drawing);
for (int i = 0; i < panel1.Width; i += 10)
Gpanel.DrawLine(blackPen1, i, panel1.Height, i, 0);
for (int i = 0; i < panel1.Height; i += 10)
Gpanel.DrawLine(blackPen1, 0, i, panel1.Width, i);
Gpanel.DrawLine(blackPen2, 0, panel1.Height - 90, panel1.Width, panel1.Height - 90);
panel1.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
private void btnSave_Click(object sender, EventArgs e)
{
drawing.Save("c:\\test.png", ImageFormat.Png);
}
}
}
|
|
|
|
|
Here's a program which creates 100 million random numbers, then counts how many are divisible by seven using three different methods:
static void Main(string[] args)
{
Random r = new Random(Environment.TickCount);
int[] numbers = new int[100000000];
for (int index = 0; index < numbers.Length; index++)
{
numbers[index] = r.Next();
}
Stopwatch sw = Stopwatch.StartNew();
int count = 0;
foreach (int number in numbers) if (number % 7 == 0) count++;
Console.WriteLine("Simple Iteration {0} : {1}", count, sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
var x = from g in numbers where g % 7 == 0 select g;
count = x.Count();
Console.WriteLine("LINQ Query {0} : {1}", count, sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
int z = numbers.Count(g => g % 7 == 0);
Console.WriteLine("Count Query {0} : {1}", z, sw.ElapsedMilliseconds);
Console.ReadLine();
}
If you run it, you'll find that the Select query(2nd) takes almost exactly twice as long as basic iteration (1st), the count method (3rd) takes about three times as long.
This is a similar program to one I wrote when LINQ came out, and it rather put me off. Not only did the LINQ look more confusing, but it took twice as long to execute. It's also harder to debug, of course.
So, interested in views on this. Can anyone adjust the app so the performance becomes similar? Because, unless I'm missing something, half the performance is a serious issue in a real-time system and something to avoid everywhere else.
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: If you run it, you'll find that the Select query(2nd) takes almost exactly twice as long as basic iteration (1st)
I'm not surprised. LINQ sacrifices speed for convenience. I'm using it a lot in my apps, and unless we're talking about a thight loop, it's not noticeable.
Rob Philpott wrote: half the performance is a serious issue in a real-time system One does not build real-time systems in a managed language. If speed is that much of an issue, you'd be better served with C++ or Delphi. Do an IntToStr in Delphi and in C#, and you'll have an equally large "problem".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: One does not build real-time systems in a managed language
I can assure you they do, I've worked on several such systems. And why not? Once the JIT is out the way calculations can be sometimes quicker than native code (I once had this in a C# vs C++ battle calculating prime numbers. The JIT can optimise for the installed processor, in my case an AMD chip and the managed code was quicker). The impact of things like array checking and garbage collection are usually negligible.
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: The JIT can optimise for the installed processor It can, but it doesn't. Ok it does (it uses FISTTP and MOVQ), but not nearly enough.
Also, so can native code, although not as fully because it's not at runtime (it takes a lot of space and it will be frozen in time): CPU dispatch is a very common technique.
Rob Philpott wrote: the managed code was quicker) That just means the native code wasn't compiled right or the compiler sucked (MSVC sucks, except maybe the 2012 version) or both. There is no excuse for it otherwise.
|
|
|
|
|
Possibly so, and you can argue the finer points but the central point is that managed languages/environments are capable.
Regards,
Rob Philpott.
|
|
|
|
|
Ok, fair enough. It's just, that "JIT can optimize for installed process"-meme that keeps going around. It's technically true and all, but if it can do something and then it doesn't do it, well..
|
|
|
|
|
Rob Philpott wrote: And why not? Because the way Windows multitasks. You're not even guaranteed to get CPU-time.
What do you define as "real-time"? If you cannot guarantee a reaction within milliseconds, we'd be talking about "instant", not "real-time". As in "instant messaging"
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have found that sometimes LinQ is slower than traditional for loops, with your example being on that I have seen first Hand too.
But recently I compared doing a product of calculation of a series of decimal numbers both with LinQ and a for loop for a project that I am working on. The LinQ for me out performed the
List<int> data = new List<int>();
for (int i = 0; i < 100; i++) data.Add(i);
Stopwatch sw = Stopwatch.StartNew();
int value = 1;
for (int i = 0; i < 99; i++)
{
value *= data[i];
}
Console.WriteLine(";Simple Iteration time {0}", sw.Elapsed.TotalMilliseconds);
sw = Stopwatch.StartNew();
int linqAgg = data.Aggregate((first, second) =>; first * second);
Console.WriteLine("Linq Iteration time {0}", sw.Elapsed.TotalMilliseconds );
Console.ReadLine();
my results are as follow
Simple Iteration time 1.7021
Linq Iteration time 0.6478
My personal feeling for the use of LinQ is to use with caution when speed is the most important factor.
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
There's a few things I think you need to take into acccunt there. Firstly, if you up the 100 to 100 million, the simple iteration is three times as quick (on my computer). When you time a small loop with 100 items in it, really you're just timing the JIT compilation time, not the actual execution time which will be super sub-millisecond. The LINQ has been pre-jitted as its part of the .NET framework so you'd expect that (the first time). Also, timers aren't that accurate so really I think you need large numbers of iterations to be able to draw comparisons, and when you do that, again, LINQ is several times slower.
Regards,
Rob Philpott.
|
|
|
|
|