|
Hi All,
I am creating an application that takes a sample every 10 ms in C#. Well I am using the Windows forms timer and its not accurate at all. Does anyone have a timer they can suggest that is more accurate? Please let me know of the timer and how to program it.
Thanks,
Laura
|
|
|
|
|
Laura - have a read through this[^] article by Luc. It might offer you the help you want.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
It's been a long time with no coding done. I need to read an MDB file pull out the "getname" then read a directory and compare the name in getname to the file names in a directory. If it's a match then move the file to another directory if it does not match move it to a different directory.
Any ideas on error handling and where or not I should use a temp directory to move the files to first just in case additional files are written to the directory from the host system.
Thanks to all
|
|
|
|
|
The DirectoryInfo class and the FileInfo class should be all you need for the file side.
DirectoryInfo dir = new DirectoryInfo(@"c:\");
FileInfo[] fileList = dir.GetFiles("*.*");
Now fileList array has a snapshot of all the files found on c:\. You can change the file filter to be whatever type of file you want. The GetFiles() method has an overload that takes an additional parameter called SearchOption that can be used to include subdirectories if you want.
FileInfo[] fileList = dir.GetFiles("*.*", SearchOptions.AllDirectories);
Once you have that snapshot, you don't care about new files added to the directory, because you'll be iterating the fileList array and the Excel doc. Hope that helps you and shows you how to avoided moving files to a temp directory when it's not needed.
|
|
|
|
|
Thanks, I'll start with that.
|
|
|
|
|
I am writing an Excel program using C# to populate each worksheet with data from a remote machine in my workbook. Depending on the name of the worksheet I need to connect to the different servers to gather the LUN(disk size info) information. The LUN information is too complicated to gather using SQL. Also, the vendor doesn't want to supply me with the SQL script. The vendor had supplied the HSMDiskStatus.exe that can gather these information. The HSMDiskStatus.exe can be execute with the following switches:
HSDDiskStatus.exe /f e:\DPSan_Chart\" + servername + ".txt"
the /f switch tell the executable where to save the txt file.
I am using the Process class to do this but I am not having any luck. I either get "parameter is invalid" or the "specified can't be found." Is there a way to check if you are connected to the remote server using Process class during debug?
thank you very much,
Donjuanwu
Below is my code:
private void ConnectToDataServer(string servername)
{
try
{
//server and directory when connect
string execPath = @"\\" + servername + @"\E:datacenter\";
string filePath = execPath + "HSMDiskStatus.exe";
string password = "password";
Process proc = new Process();
//******don't know if Proc.StartInfo.Arguments is where I need to set the switches to execute the
********HSMDiskStatus.exe
proc.StartInfo.Arguments = " " + @"/F e:\DPSanChart\" + servername + ".txt";
//switches to include when executing HSMDiskStatus.exe
string cmdLine = @"/F e:\DPSanChart\" + servername + ".txt";
proc.StartInfo.Domain = servername;
proc.StartInfo.FileName = filePath;
proc.StartInfo.UserName = @"domain\username";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Password = secret;
proc.StartInfo.WorkingDirectory = execPath;
//*********I can't inclue any arguments in Proc.Start(). I have to use
***********System.Diagnostics.Process.Start()
System.Diagnostics.Process.Start("HSMDiskStatus.exe " + cmdLine);
//proc.Start();
while (!proc.HasExited)
{
proc.Refresh();
}
proc.Close();
}
catch (Exception ex)
{
throw ex;
}
}
|
|
|
|
|
Put the arguments in the System.Diagnostics.ProcessStartInfo's Arguments property.
-- modified at 17:06 Tuesday 18th September, 2007
On further review, you can provide the arguments as the second parameter to System.Diagnostics.Process.Start(string,string)
-- modified at 17:08 Tuesday 18th September, 2007
|
|
|
|
|
Hi PIEBALDconsult,
I still get the same error when I comment out the proc.StartInfo.Argument and provided the System.diagnostics.start(string, string) when I step through the code. The error is "the system can't find the specified file."
My connection info:
Server to connect to: servername
directory where HSMDiskStatus.exe reside: E:\datacenter
executeable name: HSMDiskStatus.exe
switches: /F e:\DPSanChart\Servername.txt
string execPath = @"\\" + servername + @"\E:\datacenter\";
string filePath = execPath + "HSMDiskStatus.exe";
string password = "password";
Process proc = new Process();
//string cmdLine = @"/F E:\DPSanChart\" + servername + ".txt";
proc.StartInfo.Domain = servername;
proc.StartInfo.FileName = filePath;
proc.StartInfo.UserName = @"domain\username";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Password = secret;
proc.StartInfo.WorkingDirectory = execPath;
System.Diagnostics.Process.Start("HSMDiskStatus.exe ", @"/F E:\DPSanChart\" + servername + ".txt");
while (!proc.HasExited)
{
proc.Refresh();
}
proc.Close();
|
|
|
|
|
Then specify the full path of the EXE.
|
|
|
|
|
The execPath is invalid for a WorkingDirectory. Just don't put one, or make it "C:\".
|
|
|
|
|
That's certainly true of the line:
string filePath = execPath + "HSMDiskStatus.exe";
But that line isn't really being used. This line:
System.Diagnostics.Process.Start("HSMDiskStatus.exe ", @"/F E:\DPSanChart\" + servername + ".txt");
Is being used, and the OS doesn't find "HSMDiskStatus.exe " in the current directory. (Should probably get rid of that SPACE too.)
|
|
|
|
|
Unless I'm mistaken, the UNC path
string execPath = @"\\" + servername + @"\E:\datacenter\";
is illegal. You can't have a colon in the share name, so the path is invalid.
|
|
|
|
|
True, but I don't think he's using that value at this time... but I sure wish the code were cleaned up. I've seen some pretty messy snippets on here today.
|
|
|
|
|
Oh crap! I saw that he went through the trouble of putting a ProcessStartInfo together and then I thought actually used it...
|
|
|
|
|
Dave,
I took out the colon: in the UNC path. I have included the path and directory in the System.Diagnostics.Process.Start(). I am getting a new error when I try this: "No process is associated with this object." I physically went to the directory and execute the HSMDiskStatus.exe w/o any problem. How do I know whether my code connected to the remote machine or it's looking in my local machine for the HSMDiskStatus.exe?
Here is my code:
string password = "password";
Process proc = new Process();
proc.StartInfo.Domain = servername;
proc.StartInfo.FileName = "HSMDiskStatus.exe";
proc.StartInfo.UserName = @"domain\user";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Password = secret;
System.Diagnostics.Process.Start(@"\\" + servername + @"\E$\datacenter\HSMDiskStatus.exe ", @"/F E:\DPSanChart\" + servername + ".txt");
while (!proc.HasExited)
{
proc.Refresh();
}
proc.Close();
|
|
|
|
|
donjuanwu wrote: How do I know whether my code connected to the remote machine or it's looking in my local machine for the HSMDiskStatus.exe?
The code is loading the .EXE from the path specified by "\\servername\E$\datacenter\HSMDiskStatus.exe". Are you trying to get the process to run on the local machine, or are you trying to get the remote machine to run this HSMDiskStatus??
If you're trying to get the remote machine to run this app, you're wasting your time. You cannot use the Process class to get a remote machine to launch an app.
|
|
|
|
|
when you said the code is loading the .EXE from the path, are you saying the code is copy the .EXE from the specified path and execute the .EXE from the local machine? I would like to execute the .EXE from the remote machine since the .EXE is in the remote machine. do you know of a class that I can use to execute the .EXE from the remote machine?
Thanks,
|
|
|
|
|
donjuanwu wrote: when you said the code is loading the .EXE from the path, are you saying the code is copy the .EXE from the specified path and execute the .EXE from the local machine?
The .EXE is being loaded and run on the local machine, just as if you ran it from your own hard drive.
donjuanwu wrote: I would like to execute the .EXE from the remote machine
You mean execute the .EXE ON the remote machine?? No, there is no .NET class that does this because it's a HUGE security risk.
It CAN be done though, using WMI's Win32_Process[^] class, though with very large restrictions. The process you launch cannot be interactive, meaning there will be no user interface showing on the remote machine, and it runs with limited access to machine resources.
|
|
|
|
|
What I've tried (and I think it worked) is writing a BAT file on the remote system that will run the EXE and then use SCHTASKS to create a task on the remote system to run it. Total Kludge, but it could solve the problem. Still no interface of course.
|
|
|
|
|
Yeah, I'v seen PSEXEC being used too. The problem with that is it's on everybody's virus scanners "Potentialy Unwanted Program" list.
If all he's looking for is the disk space information, it's far easier to use WMI to get it than running a remote proc.
|
|
|
|
|
if I don't include the path and the filename"
string execPath = @"\\" + servername + @"\E:\datacenter\";
string filePath = execPath + "HSMDiskStatus.exe";
proc.StartInfo.FileName = filePath;
how would the code know to connect to the remote server? Also, I comment out the code line:
// proc.StartInfo.WorkingDirectory = execPath;
I still get the incorrect parameter error.
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Password = secret;
// proc.StartInfo.WorkingDirectory = execPath;
System.Diagnostics.Process.Start("HSMDiskStatus.exe ", @"/F E:\DPSanChart\" + servername + ".txt");
|
|
|
|
|
Dave,
I have tried the following and I still not able to make it work:
// proc.StartInfo.WorkingDirectory = @"C:\";
proc.StartInfo.WorkingDirectory = @"C:\";
proc.StartInfo.WorkingDirectory = @"E:\datacenter";
I still get the parameter is in correct.
|
|
|
|
|
Use PIEBALDconsultant's suggestion and PLEASE change that throw ex; in your catch to just be throw; If you rethrow the exception the way you are doing it, you will hide the stack trace.
Process also has a FileName property that you can use to set the path/name of the executable. So if you use FileName property with the Arguments, you only need to do a Process.Start() without any parameters.
|
|
|
|
|
Hi Chris,
I changed my code to use the Process.StartInfo.Argument to set the command lines and I still get an error. My error is "the parameter is incorrect."
private void ConnectToDataServer(string servername)
{
try
{
string execPath = @"\\" + servername + @"\E:\datacenter\";
string filePath = execPath + "HSMDiskStatus.exe";
string password = "password";
Process proc = new Process();
proc.StartInfo.Domain = servername;
proc.StartInfo.FileName = filePath;
proc.StartInfo.Arguments =@"/F e:\DPSanChart\" + servername + ".txt";
proc.StartInfo.UserName = @"domain\user";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Password = secret;
proc.StartInfo.WorkingDirectory = execPath;
proc.Start();
while (!proc.HasExited)
{
proc.Refresh();
}
proc.Close();
}
catch (Exception ex)
{
throw;
}
On a different note what is the advantage of seeing the stack. What is the stack? I look at the detail of the error within the catch block and the error looks the same to me...)
|
|
|
|