|
Hi All,
Can any one give me the suitable functions for the following in c# :
1. To find first instance of a file name that matches the file specification like *.jpg? like _tfindfirst( ) function in vc++
2.To find the next name, if any, that matches the file specification? like _tfindfirst( ) function in VC++
3. To find the Sub Directory? like _A_SUBDIR( ) function in vc++
4. To ignore directory with . and .. ?
Thanks
|
|
|
|
|
Fore more file specification, please read FileInfo Help.
The subdirectory . and .. are ignored.
static FileInfo FindFile(string startDirectory, string extension)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(FileInfo fi in di.GetFiles())
{
if(fi.Extension.ToLower() == extension)
return fi;
}
return null;
}
static DirectoryInfo FindDirectory(string startDirectory, string dirNameToSearch)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(DirectoryInfo diSearch in di.GetDirectories())
{
if(diSearch.Name == dirNameToSearch)
return diSearch;
}
return null;
}
|
|
|
|
|
|
Dim strSQL
strSQL = "SELECT * FROM MyTable WHERE username = '" & _
Request.Form("username") & "'"
Set rs = db.Execute()
If rs.EOF Then
'Now do the insert
Else
Response.Write "Record exists"
End If
Can anybody help me using this code but in c#? I just need some kind of function that verify if I have already that name in database...but can't find the execute method in VB
Thanks
|
|
|
|
|
Are you talking about moving from ASP (not .NET) to C#?
cuz if, you are going to need to do a little more than this.
I can translate it to you but I don't think that this will work good.
string strSQL = "SELECT * FROM MyTable WHERE username = '" +<br />
Request.Form["username"] + "'";<br />
<br />
if (db.Execute().Rows.Count != 1)
else<br />
Response.Write ("Record exists");
good luck
|
|
|
|
|
Obviously parameters should be used. If not, people entering usernames like
' OR 1 = 1 --
can do funny things in your system. And this is such a simple way to hack a system that you can rest assured every single wannabee hacker out there knows it.
|
|
|
|
|
The purpose of this email to hopefully gain some insight into implementing a javascript scripting engine within an application framework for a .Net application. I am trying to figure out how to provide scripting capabilities to an application. I would like to provide a scripting engine that enables customers to implement their own business rules. I would like to run custom javascript scripts that handle certain events (OnBeforeUpdate or OnBeforeDelete or OnBeforeAdd, OnAfterAdd, etc...) on different business objects.
I would like to stick with javascript because it's a widely used scripting language. I am just not sure how to go about implementing a javascripting engine into my .Net application framework.
I took a look at Visual Studio for Applications (VSA) which seemed like a possible option but the fact that it's being discontinued has lead to me seek out other options. Again, I would like to stick with a javascript solution.
Any ideas on how to get started with this?
Any thoughts or guidance on this topic would be greatly appreciated. TIA!
Regards,
-Derek
-- modified at 21:57 Tuesday 23rd May, 2006
|
|
|
|
|
I am trying to calculate how many lines I can fit on a printed page using the following code. However, as you can see from the times i put against the trace statements, it is very slow indeed.
private void SetLinesPerPage()
{ this.pd = new PrintDocument();
Trace.WriteLine("SetLinesPerPage: Point 0"); // **TEST 21-May-2006 10:11:18.703
PageSettings ps = this.pd.DefaultPageSettings;
Trace.WriteLine(string.Format("{0}:{1}:{2}", ps.HardMarginX, ps.HardMarginY, ps.Bounds.Height));
// **TEST 21-May-2006 10:11:24.109
Graphics g = ps.PrinterSettings.CreateMeasurementGraphics();
Trace.WriteLine("SetLinesPerPage: Point 2"); // **TEST 21-May-2006 10:11:26.906
float pHeight = ps.PrintableArea.Height;
Trace.WriteLine("SetLinesPerPage: Point 3"); // **TEST 21-May-2006 10:11:28.578
this.pFont = new Font(pfName, pfSize);
float lHeight = this.pFont.GetHeight(g) * 1.5F;
this.pFont.Dispose();
this.linesPerPage = (int)(pHeight / lHeight) - 5;
Trace.WriteLine(string.Format("SetLinesPerPage: Set {0} lines per page", this.linesPerPage));
}
My printer is a USB connected device on another PC in my workgroup. It is online but I have not accessed it since I booted my machine. If I do access it before running this code, then it still takes the same elapsed time.
My systems are both Windows XP SP2 (fully up to date), both machines are running .NET V2 framework.
Please does anyone have any idea how I can descrese the elapsed time to perform this operation? (currently almost 10 seconds)
Thanks in advance.
|
|
|
|
|
hi
i want to use serial port in my project
how can i control the serial port with Visual Studio 2003
|
|
|
|
|
|
This is the source code from someone. I forgot the link to the right article.
This article is in VB.NET : http://www.codeproject.com/vb/net/Comport_made_simple.asp
but it was ported to C#.
using System;
using System.Runtime.InteropServices;
namespace Communication
{
public enum Parity
{
No = 0,
Odd,
Even,
Mark,
Space
}
public class CommPort
{
public int PortNum;
public int BaudRate;
public byte ByteSize;
private byte parity;
public byte StopBits;
public int ReadTimeout;
private int hComm = -1;
public bool Opened = false;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
public Parity Parity
{
get
{
switch (parity)
{
case 0:
return Parity.No;
case 1:
return Parity.Odd;
case 2:
return Parity.Even;
case 3:
return Parity.Mark;
case 4:
return Parity.Space;
default:
return Parity.No;
}
}
set { parity = (byte) value.GetHashCode(); }
}
[StructLayout(LayoutKind.Sequential)]
public struct DCB
{
public int DCBlength;
public int BaudRate;
public uint flags;
public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public char XonChar;
public char XoffChar;
public char ErrorChar;
public char EofChar;
public char EvtChar;
public ushort wReserved1;
}
[StructLayout(LayoutKind.Sequential)]
private struct COMMTIMEOUTS
{
public int ReadIntervalTimeout;
public int ReadTotalTimeoutMultiplier;
public int ReadTotalTimeoutConstant;
public int WriteTotalTimeoutMultiplier;
public int WriteTotalTimeoutConstant;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool GetCommState(
int hFile,
ref DCB lpDCB
);
[DllImport("kernel32.dll")]
private static extern bool SetCommState(
int hFile,
ref DCB lpDCB
);
[DllImport("kernel32.dll")]
private static extern bool GetCommTimeouts(
int hFile,
ref COMMTIMEOUTS lpCommTimeouts
);
[DllImport("kernel32.dll")]
private static extern bool SetCommTimeouts(
int hFile,
ref COMMTIMEOUTS lpCommTimeouts
);
[DllImport("kernel32.dll")]
private static extern bool ReadFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
hComm = CreateFile("COM" + PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hComm == INVALID_HANDLE_VALUE)
{
throw(new ApplicationException("Comm Port Can Not Be Opened"));
}
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, ref ctoCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.flags = 0;
dcbCommPort.flags |= 1;
if (Parity > 0)
{
dcbCommPort.flags |= 2;
}
dcbCommPort.Parity = parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
throw new Exception("Comm Port Can Not Be Opened");
}
Opened = true;
}
public void Close()
{
if (hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
}
}
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, OutBytes, BytesRead);
}
else
{
throw(new ApplicationException("Comm Port Not Open"));
}
return OutBytes;
}
public void Write(byte[] WriteBytes)
{
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
}
else
{
throw(new ApplicationException("Comm Port Not Open"));
}
}
}
}
-- modified at 2:26 Wednesday 24th May, 2006
|
|
|
|
|
thank you guys
good coding
|
|
|
|
|
DA1 = new SqlDataAdapter("select * from tbmohasebe where tarikh = '"+comboBox1.Text +"'" , conn1);
DA1.Fill(dt1); //dt1 is Data table
dataGridView1.DataSource = dt1;
how can i hide some of columns in datatable that don't show in datagrid?
|
|
|
|
|
Use a specific select list...
select field1, field2, field3 from Table where blah blah blah...
Visit BoneSoft.com
|
|
|
|
|
Isn't this working ?
dt1.Columns.Remove("columnName");
------------------------------ "The Soapbox has been so ..."
|
|
|
|
|
On load of your form or in any another event u can add code down below
dataGridView1.Columns[index].Visible = false;
//indexn is index of column which u want to hide in dataGridView
|
|
|
|
|
I created an mdi form, and then added three children to it.
When I click on any child form it becomes the active one and its title becomes bluish to indicate that.
The issue that I have is that when I click on any child form via right mouse click, I am not able to get that form to be the active or the selected form. How can I activate a child form via right mouse click only?
Thanks.
|
|
|
|
|
I have written a Windows Service which creates multiple threads. Each thread listens on a seperate port, writes received messages to disk and acknowledges the receipt of each message. It works perfectly with a single thread so I started load testing multiple threads with disapointing results. I've noticed erratic behavior and am pretty sure its due to the multi-threading. This is my first true multithreaded app and wanted to get some pointers. My Windows Service basically just creates (with ThreadPool.QueueUserWorkItem) and stops the threads as necessary. The rest of the code (Listening and responding, storing port numbers and directory paths, writing messages to disk) is contained in a DLL shared by each thread.
So first question is:
1. Is what I'm attempting to accomplish possible, or is it going to be a locking nightmare ?
The reason I ask is that it appears I need to lock the listening loop since it is contained in a DLL used by each thread. But won't this effectively stop the rest of the the threads from listening to their ports since it is blocking?
Do I need to ditch the idea of listening on multiple ports within threads and just have multiple instances of the same app on a server ?
Any help would be appreciated.
|
|
|
|
|
You know, it might help to look at this article[^].
It looks kinda like what you're trying to do...but it seems like he's done all the hard work for you.
Once you wanted revolution
Now you're the institution
How's it feel to be the man?
|
|
|
|
|
Hello,
I try to write these characters to an XML file. but in the XML file it shows:
> < & instead. how can i write correctly to the XML file or at least read correctly(assign the innertext to a variable)?
here is my code for writing to XML file:
XmlElement title= xmldoc.CreateElement("Title");
title.InnerText = txtName.Text;
newBook.AppendChild(title);
Donkaiser
|
|
|
|
|
haha but in the XML file it shows:
& gt; & lt; & amp; instead. So confusing!!!
Donkaiser
|
|
|
|
|
there are some characters that has special meaning in html/xml files, for example <, >, & and so on. So, if you need to display such character in xml/html file you should use one of the special form of symbol writing, for example '&' followed by character short form and followed by ';'. '&' gt ';' for 'greater' sign.
When you are reading html/xml file you have to decode such symbols.
In your case, if you have only this three symbols in your file - you can make simple replace, either through string.replace(), either through regex.replace(). But if you can get more symbols (probably, all special chars) - then use standart functions - parse xml file into set of xml element object (or any other way) and read it's InnerText property.
Notice, that if you'll write special chars into InnerText, and read it from it - It'll be ok.
XmlDocument doc = new XmlDocument();<br />
doc.LoadXml("<root>" +<br />
"<elem>some text<child/>more text</elem>" +<br />
"</root>");<br />
<br />
XmlElement elem = (XmlElement)doc.DocumentElement.FirstChild;<br />
elem.InnerText = "testing < > &";<br />
MessageBox.Show(elem.InnerText);
MessageBox shows "testing < > &"
____________________________________________
Robin Panther http://www.robinland.com
|
|
|
|
|
Using innerText format automatically the text.
Donkaiser
|
|
|
|
|
I'm stuck on this one, really bad.
Exact Error Message:
An unhandled exception of type 'System.Security.SecurityException' occurred in CheckPageUpdates.exe
Additional information: Request failed.
If I run my application from my local box it works perfectly. If I run the app off of the Network it fails giving the above error message.
Now I know what this issue normally is, it's about not having your Machine->LocalIntranet_Zone setup to allow your program to run with enough rights. So I created a key using sn and added it to the assembly, built my program, then i loaded the key into my Machine->All_Code by adding another group and give it full permissions. This seemed to solve nothing. My program has about 10 lines and it communicates with a webservice which I also wrote.
CODE:
static void Main() <br />
{ <br />
new CheckPageUpdates();<br />
}<br />
<br />
public CheckPageUpdates()<br />
{<br />
try<br />
{<br />
PageModified.PageModifiedService pms = new PageModified.PageModifiedService();<br />
<br />
string strAddress = pms.CheckPageUpdated(Environment.GetEnvironmentVariable("username"));<br />
<br />
if(strAddress != string.Empty)<br />
{<br />
Process iexplore = new Process();<br />
<br />
iexplore.StartInfo.FileName = "iexplore.exe";<br />
iexplore.StartInfo.Arguments = strAddress;<br />
<br />
iexplore.Start();<br />
}<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.ToString()); <br />
}<br />
}
What this does is it hands off the username of the currently logged in user to the webservice which then checks to see if this user has seen a webpage recently enough or if they need to see it again. It then returns a web address, or string.Empty, depending on whether or not they should load a page.
When the code fails and I load the debugger it shows me that it failed at the bolded underlined point.
Thanks a bunch for at least reading this
--Peter
|
|
|
|
|
In my experiencing, certain operations (especially those relating to files / directories) are not allowed or require extra security permissions to use in .NET.
At an educated guess:
string strAddress = pms.CheckPageUpdated(Environment.GetEnvironmentVariable("username"));
is probably causing it.
To suppress it you will need to have your application request additional permissions. This is done through the System.Security namespace, but I am unsure to the specifics of it.
Hopefully I have given you enough information to googlise
James
just-code-it.net
|
|
|
|
|