|
It depends, if the files can be edited by a malicious user then a CRC will not be good enough since it's very easy to calculate collisions for them. SHA2 would be OK, but there is still a chance that two different files will accidentally have the same hash (which follows from the fact that there are more different files than there are hashes, since the hash has a small fixed length), that chance is rather small for meaningful files though.
If it's very extremely important that the files are actually the same (with zero chance on false positives) then there are no shortcuts and you'd have to compare them byte-by-byte (edit: but of course you should read the file block-by-block, as said above, it would be rather braindead to read just a single byte many times in a loop)
|
|
|
|
|
You have to define when files are the same to you.
In one way, if two files exist, they are always different: they may have identical creation date, modification date, length, and content; however when they have identical names, they are residing in different folders or partitions. So be more specific.
Once defined, you can perform identity checking by checking the attributes that are relevant to your definition; for content it is wise to calculate (and probably store) some kind of hash, their is an infinite number of definitions and algorithms; Windows Explorer itself holds one 32-bit CRC for file content; ZIP files hold another one. Hashes and CRCs will be identical when content is identical, and they are very likely to be different for different content; when that isn't good enough, you need to compare all the bytes.
|
|
|
|
|
A byte-by-byte compare will likely take less time than calculating two CRCs or hashes (especially if the compare returns false early), so the only gain is if you store the CRC or hash for later. But you need to be sure that the file hasn't changed since you calculated its CRC or hash.
jbradshaw wrote: keeping a list of files in a database
Are you storing the actual file content? Or just the path and other information?
If you only store the path I wouldn't trust that the file has not been changed (or even deleted) so I wouldn't bother storing the CRC or hash.
Personally, I store the file content and a SHA1 hash.
<Anecdote>
My GenOmatic[^] generates a file and I want to know if the new file matches the previous version. I considered storing a hash in the file and comparing, but quickly decided that it was too unreliable so I went with a string compare.
</Anecdote>
|
|
|
|
|
I know the files won't change once I have calculated the CRC for them.
All I really need is a unique number on the contents of the file so that later if I have two files, I can compare those numbers instead of having to do a byte by byte comparison.
Here's what I'm trying to do in a nutshell.
Somebody uploads a file to my website.
I store the file for long term storage ( just go with it ).
Somebody uploads another file to my website.
If the file is the same as any of the files I've already stored, don't store it again otherwise store the file.
Somebody uploads another file to my website.
If the file is the same as any of the files....
So really I want to make sure I only have distinct files on the server. I don't have a problem with it being uploaded multiple times, I'd just like to have it so that the file is retained(stored) only once.
I figured by doing some kind of CRC check, I could get the CRC when the file is uploaded and then check the DB for any other files with the CRC. If not, add it to the DB with the CRC and go one. If the file already exists, don't bother saving it and delete the file from the upload area.
TIA - Jeff.
|
|
|
|
|
Makes sense to me; I'd store a hash and file length.
|
|
|
|
|
Can anybody suggest or point me to a hash routine?
TIA - Jeff.
|
|
|
|
|
Here's what I use, the result is a forty-character string:
public static string
Hash
(
string Subject
)
{
return ( Hash
(
System.Text.Encoding.Unicode.GetBytes ( Subject )
,
new System.Security.Cryptography.SHA1Managed()
) ) ;
}
public static string
Hash
(
byte[] Subject
,
System.Security.Cryptography.HashAlgorithm Provider
)
{
System.Text.StringBuilder result =
new System.Text.StringBuilder ( Provider.OutputBlockSize ) ;
foreach
(
byte b
in
Provider.ComputeHash ( Subject )
)
{
result.Append ( b.ToString ( "X2" ) ) ;
}
return ( result.ToString() ) ;
}
You can choose another hash algorithm and you don't have to convert it to a string.
|
|
|
|
|
Hi,
in my Visual Studio2008 Setup and Deployment Project
am trying to disable the repair radiobutton from the installation package.
for that am using ocra to edit the msi tables.
in 'RadioButton' table i deleted
1. MaintenanceForm_Action
2. AdminMaintenanceForm_Action
but at the time of unistallation it is giving error 'The installer has encounteres an unexpected error installing this package. This may indicate a problem with this package. The error code is 2855'
what more things i need to do??
please suggest me
i dont want Repair button finally in my installation package.
please help me out
Thanks In Advance
--Naren
|
|
|
|
|
Hi All I have a project and I want to add another project to it when the installer
install the project it should list both project
on the programe menu. So, user can select either one to run.
FYI: the first project in VC++ and the second one is C# both windows application.
Base Programe Folder:
1. App one
2. App two
please any idea or hint would help. Or any codeproject articale
thanks
|
|
|
|
|
|
Hi, guys, I have a C# struct array and I need to pass it to C++ code and get it filled with necessary data. Here is my C# code:
public struct DataStruct
{
public string dataName;
public int slot;
public int quad;
public DataStruct(string name, int slot, int quadrant)
{
this.dataName= name;
this.slot = slot;
this.quad = quadrant;
}
}
// C++ function
[DllImport("CppCode.dll")]
public static extern int GetDataList([In, Out] DataStruct[] dataList);
// Create the struct array
DataStruct[] dataList = new DataStruct[512];
// Call C++ function
GetDataList(dataList);
C++ code in CppCode.dll:
struct DataStruct
{
char * dataName;
int slot;
int quad;
};
DLLEXPORT int GetDataList(DataStruct* dataList)
{
for (int i = 0; i < 100; i++)
{
dataList[i].dataName = "qqq";
dataList[i].slot = i;
dataList[i].quad = i+1;
}
return 0;
}
Please help to find out if I did something wrong. The most strange thing is that this code works fine in some computers but in other computers, it crashes when return back from C++ code.
Thanks a lot!!
|
|
|
|
|
you should transfer strings between C# and C++. Use a char[].
Quick rule: use native data types for interfaces. (Use them everywhere erverytime and forever)
Never ever use objects or class types.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
songll wrote: dataList[i].dataName = "qqq";
This will not work, you cannot put a pointer where C# has a string. Also the pointer will no longer be valid on return to the C# code. You need to provide a character array (Unicode) and the C++ code will need to copy the text into the array.
|
|
|
|
|
Thanks, Richard, Can you give me a detail example on how to copy this?
|
|
|
|
|
songll wrote: Can you give me a detail example on how to copy this?
Not exactly. You need to modify your C# structure to contain only basic types that will map directly to C++ types. You can then use the wcscpy_s[^] function to copy text into the structure. See also Interoperating with Unmanaged Code[^], for more details and examples.
|
|
|
|
|
May be an old issue But I am not able to solve.
I have a Microsoft reportviewer report in my application. The report loads tables from a datatable and loads some other values from my object class properties. So far OK
I have a image control in my report. I have to load a image from my application folder at runtime. cannot use embedded as the image is being changed before every time report loads.(Image is actually a graph image from zedGraph)
How to do it. I saw some posts but cant get the idea clearly.
please help
Thanks
With Regards
Roy Thomas
"..this file is known as source file probably because it is a source of frustration and anxiety!" - Chuck Sphar - In book 'C# 2005 for Dummies'.
|
|
|
|
|
You can get the path to the folder that your app was launched from using Application.StartupPath and build the fully qualified path to your file using the Path class:
string fullPath = Path.Combine(Application.StartupPath, "myfilename.bmp");
|
|
|
|
|
Possible solutions :
- modifying the registry in HKCU
- modifying the registry in HKCR
- using ClickOnce
So far I haven't been able to find a method that actually works with all 3 OS's.
Does somebody have some example code how to go about this ?
(Everybody always points to the FileAssociation code on this site, but that doesn't work under Win7)
|
|
|
|
|
Why would you need code to do something that only needs to be done once?
Doesn't work on Windows 7?? Worked fine for me...
|
|
|
|
|
hello there...
I need to do a graphic like this one:
http://zedgraph.org/wiki/index.php?title=Stacked_Bar_With_Labels_Demo_v4[^]
but the x axis values need to be date and the first value is the current datetime.
I am using the code you find in the link, but added this line:
myPane.XAxis.Type = AxisType.Date;
myPane.XAxis.Min = (double)new XDate(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")));
and the x1,x2,x3 variables set with
(double)new XDate(.....)
can someone help me??
sorry my English! :P
|
|
|
|
|
You are better off asking the vendor
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Zedgraph is free, so there is no vender...
anyway, i will try theirs website.
|
|
|
|
|
It didn't just materialize out of thin air. SOMEONE had to write it! There is a discussion forum on the project site.
|
|
|
|
|
looool
really ?
I think i got it.. wrong BarType
thanks!
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:58am.
|
|
|
|