I have an application which uses INI files for start up.
Multiple instances on of the same application with different INI files configuration.
This also results in multiple instances with same INI file can be started. I want to restrict only this case but multiple instance with different INI file must be allowed. what is the best way to achieve this?
Create a Mutex with a name based on the ini file (MD5 of the file name or content). If the Mutex already exists, it means the application is already started with the specified ini file.
public static string CalculateMD5Hash(string input)
{
using (MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
static void Main(string[] args)
{
using (Mutex mutex = new Mutex(true, CalculateMD5Hash(args[0])))
{
if (mutex.WaitOne(100))
{
Console.WriteLine("First instance");
Console.ReadKey();
}
else
{
Console.WriteLine("Second instance");
Console.ReadKey();
}
}
}
Related
I have a compressed file .rar .7z, .tar and .zip and I want to rename physical file name available in above compressed archived using C#.
I have tried this using a sharpcompress library but I can't find such a feature for rename file or folder name within .rar .7z, .tar and .zip file.
I also have tried using the DotNetZip library but its only support.Zip see what I have tried using DotNetZip library.
private static void RenameZipEntries(string file)
{
try
{
int renameCount = 0;
using (ZipFile zip2 = ZipFile.Read(file))
{
foreach (ZipEntry e in zip2.ToList())
{
if (!e.IsDirectory)
{
if (e.FileName.EndsWith(".txt"))
{
var newname = e.FileName.Split('.')[0] + "_new." + e.FileName.Split('.')[1];
e.FileName = newname;
e.Comment = "renamed";
zip2.Save();
renameCount++;
}
}
}
zip2.Comment = String.Format("This archive has been modified. {0} files have been renamed.", renameCount);
zip2.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
But actually the same as above I also want for .7z, .rar and .tar, I tried many libraries but still I didn't get any accurate solution.
Please help me.
This is a simple console application to rename files in .zip
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
namespace Renamer
{
class Program
{
static void Main(string[] args)
{
using var archive = new ZipArchive(File.Open(#"<Your File>.zip", FileMode.Open, FileAccess.ReadWrite), ZipArchiveMode.Update);
var entries = archive.Entries.ToArray();
//foreach (ZipArchiveEntry entry in entries)
//{
// //If ZipArchiveEntry is a directory it will have its FullName property ending with "/" (e.g. "some_dir/")
// //and its Name property will be empty string ("").
// if (!string.IsNullOrEmpty(entry.Name))
// {
// var newEntry = archive.CreateEntry($"{entry.FullName.Replace(entry.Name, $"{RandomString(10, false)}{Path.GetExtension(entry.Name)}")}");
// using (var a = entry.Open())
// using (var b = newEntry.Open())
// a.CopyTo(b);
// entry.Delete();
// }
//}
Parallel.ForEach(entries, entry =>
{
//If ZipArchiveEntry is a directory it will have its FullName property ending with "/" (e.g. "some_dir/")
//and its Name property will be empty string ("").
if (!string.IsNullOrEmpty(entry.Name))
{
ZipArchiveEntry newEntry = archive.CreateEntry($"{entry.FullName.Replace(entry.Name, $"{RandomString(10, false)}{Path.GetExtension(entry.Name)}")}");
using (var a = entry.Open())
using (var b = newEntry.Open())
a.CopyTo(b);
entry.Delete();
}
});
}
//To Generate random name for the file
public static string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
}
}
Consider 7zipsharp:
https://www.nuget.org/packages/SevenZipSharp.Net45/
7zip itself supports lots of archive formats (I believe all you mentioned) and 7zipsharp uses the real 7zip. I've used 7zipsharp for .7z files only but I bet it works for others.
Here's a sample of a test that appears to rename a file using ModifyArchive method, I suggest you go to school in it:
https://github.com/squid-box/SevenZipSharp/blob/f2bee350e997b0f4b1258dff520f36409198f006/SevenZip.Tests/SevenZipCompressorTests.cs
Here's the code simplified a bit. Note that the test compresses a 7z file for its test; that's immaterial it could be .txt, etc. Also note it finds the file by index in the dictionary passed to ModifyArchive. Consult documentation for how to get that index from a filename (maybe you have to loop and compare).
var compressor = new SevenZipCompressor( ... snip ...);
compressor.CompressFiles("tmp.7z", #"Testdata\7z_LZMA2.7z");
compressor.ModifyArchive("tmp.7z", new Dictionary<int, string> { { 0, "renamed.7z" }});
using (var extractor = new SevenZipExtractor("tmp.7z"))
{
Assert.AreEqual(1, extractor.FilesCount);
extractor.ExtractArchive(OutputDirectory);
}
Assert.IsTrue(File.Exists(Path.Combine(OutputDirectory, "renamed.7z")));
Assert.IsFalse(File.Exists(Path.Combine(OutputDirectory, "7z_LZMA2.7z")));
I'm trying to compile the code that Microsoft provides (SHA-256 hashing) however I get a myriad of issues. Namely these include error CS1065 (Unexpected character '$') and error C2061 (syntax error: identifier 'class', ';'). I'm not familiar with compiling C# programs however I have followed multiple guides to no avail.
Thank you in advance.
using System;
using System.IO;
using System.Security.Cryptography;
public class HashDirectory
{
public static void Main(String[] args)
{
if (args.Length < 1)
{
Console.WriteLine("No directory selected.");
return;
}
string directory = args[0];
if (Directory.Exists(directory))
{
// Create a DirectoryInfo object representing the specified directory.
var dir = new DirectoryInfo(directory);
// Get the FileInfo objects for every file in the directory.
FileInfo[] files = dir.GetFiles();
// Initialize a SHA256 hash object.
using (SHA256 mySHA256 = SHA256.Create())
{
// Compute and print the hash values for each file in directory.
foreach (FileInfo fInfo in files)
{
try {
// Create a fileStream for the file.
FileStream fileStream = fInfo.Open(FileMode.Open);
// Be sure it's positioned to the beginning of the stream.
fileStream.Position = 0;
// Compute the hash of the fileStream.
byte[] hashValue = mySHA256.ComputeHash(fileStream);
// Write the name and hash value of the file to the console.
Console.Write($"{fInfo.Name}: ");
PrintByteArray(hashValue);
// Close the file.
fileStream.Close();
}
catch (IOException e) {
Console.WriteLine($"I/O Exception: {e.Message}");
}
catch (UnauthorizedAccessException e) {
Console.WriteLine($"Access Exception: {e.Message}");
}
}
}
}
else
{
Console.WriteLine("The directory specified could not be found.");
}
}
// Display the byte array in a readable format.
public static void PrintByteArray(byte[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write($"{array[i]:X2}");
if ((i % 4) == 3) Console.Write(" ");
}
Console.WriteLine();
}
}
I'm a beginner at C# programming.
I wanted to create a text file to the desktop in C# Console, which is wanted to add my input new string value to the created text file's new line.
This is my work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace noteonce
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("New Word: ");
string newWord = Console.ReadLine();
string wlist = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\list.txt";
TextWriter inject = new StreamWriter(wlist, true);
inject.WriteLine(newWord);
inject.Close();
Console.WriteLine("New word has been added! ");Console.ReadKey();
}
}
}
I created the file through the console, but I want each of my entered string to be unique, I did some look up on google but I'm so confused. I want the console to tell me that whether the new input already exists or not, If it is, to warn me as "It already exists! Input another word : ", If it does not exist, just to add it to the list. I need your assistance.
Thank you all for your attention. By the help of Mr.Ankitkumar Bhatt, This is my recent work :
static void Main(string[] args)
{
string wlist = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+#"\list.txt";
FileStream create = File.Open(wlist, FileMode.Create);
create.Close();
for (int i = 0; i < 100; i++)
{
Console.WriteLine("New Word"+#" ("+(100-i)+") :");
string newWord = Console.ReadLine();
string FileContents = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\list.txt");
TextWriter inject = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\list.txt", true);
if (!FileContents.Contains(newWord))
{
inject.WriteLine(newWord);
inject.Close();
}
else
{
Console.WriteLine("It already exists!");
Console.ReadKey();
inject.Close();
}
}
}
But I want to point that, I want the program to recognize all of items in the list, by my last method, it quite works, but when I close, and open the program again, It doesn't give me the warning that New Word is already exist, doesn't add it to the file neither. How can I do the rest ?
In case of "no duplicate" please, have a look at HashSet<String>; you may find TextWriter and TextReader being too complex - try File.ReadLines(), File.AppendAllLines instead:
static void Main(string[] args) {
// better practice is paths combining
string path = Path.Combine(Environment.SpecialFolder.Desktop, "list.txt");
// unique (no duplicates) strings so far
HashSet<String> hash = new HashSet<string>(
File.ReadLines(path), // file to read from
StringComparer.OrdinalIgnoreCase); // let's ignore words' case ("World", "world")
Console.WriteLine("New Word: ");
string newWord = Console.ReadLine().Trim(); // let's trim spaces: "world " -> "world"
if (!string.IsNullOrEmpty(newWord)) // let's not add an empty string
if (!hash.Contains(newWord)) {
// add new word to the end of file
File.AppendAllLines(path, new string[] {newWord});
Console.WriteLine("New word has been added!");
}
else
Console.WriteLine("It already exists! Input another word");
else
Console.WriteLine("We don't add empty lines.");
Console.ReadKey();
}
In case you want to add several words, one after one (put an empty line to exit):
static void Main(string[] args) {
// better practice is paths combining
string path = Path.Combine(Environment.SpecialFolder.Desktop, "list.txt");
// unique (no duplicates) strings so far
HashSet<String> hash = new HashSet<string>(
File.ReadLines(path), // file to read from
StringComparer.OrdinalIgnoreCase); // let's ignore words' case ("World", "world")
while (true) {
Console.WriteLine("New Word: ");
string newWord = Console.ReadLine().Trim(); // let's trim spaces: "world " -> "world"
if (string.IsNullOrEmpty(newWord))
break;
if (hash.Add(newWord)) {
File.AppendAllLines(path, new string[] {newWord});
Console.WriteLine("New word has been added!");
}
else
Console.WriteLine("It already exists! Input another word.");
}
Console.ReadKey();
}
before injecting word check the word exists or not like below
string FileContents = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\list.txt");
if (!FileContents.Contains(newWord))
{
// Add to file //
}
this can be accomplished in multiple ways. I will present a solution that most closely will work with your code. There is definitely a more elegant way to accomplish this, but this is a quick and dirty way to accomplish that.
one way is to have a foreach check from your text file so:
var isWordPresent = false;
var textLines = File.ReadAllLines(wlist);
foreach (var line in textLines) {
if (line.contains(newWord) {
isWordPresent = true;
}
}
if (isWordPresent == false) {
inject.WriteLine(newWord);
inject.Close();
isWordPresent = false; //added this portion incase you run this code in a while loop
//so you can reuse it. You would need to have the boolean reset to false
}
1) Read the file and write its content to a string[] (Array of string):
var lines = File.ReadAllLines(wlist , Encoding.UTF8);
2) Read your input and check for duplicates:
var input = Console.ReadLine();
if (lines.Contains(input)) {
//Warning message
} else {
//Success message
}
Can someone helps me out with my problem.
I have to take a file's directory in zip file so i can calculate its MD5 hash (without unzip it). I am using DotNetZip Library but i can't find the solution of the problem. I'll show you what i've tryed and hope you will help as fast as possible.
Thanks!
if (ofd.ShowDialog() == DialogResult.OK)
{
using (ZipFile zip = ZipFile.Read(ofd.FileName))
{
foreach (ZipEntry f in zip)
{
GetMD5HashFromFile(ofd.FileName+"\\"+f.FileName);
}
}
}
The problem is that you do not extract the Zip entry, it is still in the archive. That is why it does not find the path.
I recommend to use the stream and calculate on that, without extracting.
Be aware of that MD5 is no collision safe.
You have to reference in your project the System.IO.Compression.FileSystem.dll.
Full working console application:
public class Program
{
static void Main(string[] args)
{
var z = ZipFile.OpenRead(#"C:\directory\anyfile.zip");
foreach (ZipArchiveEntry f in z.Entries)
{
var yourhash = GetMD5HashFromFile(f.Open());
}
}
public static string GetMD5HashFromFile(Stream stream)
{
using (var md5 = new MD5CryptoServiceProvider())
{
var buffer = md5.ComputeHash(stream);
var sb = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
sb.Append(buffer[i].ToString("x2"));
}
return sb.ToString();
}
}
This should be an extremely easy fix, but for some reason I am missing something. All I am trying to do is get the String Builder function that I have to write the header, but for some reason it isnt currently.
When I try to change the if statement to !File.Exists(tempFileName), it does not run through my loop.
Any suggestions? Also, let me know if you need more info. Thanks in advance.
public static void Open(string tempFileName, string division,
int zipFiles, int conversions, int returnedFiles, int totalEmails)
{
StreamWriter dailyStats;
//This is where I am missing something
//I am passing in the original filename of a log, then adding "-Stats.log"
//so I can tell the difference between what is the new stats file, and the original log file
if (File.Exists(tempFileName))
{
dailyStats = new StreamWriter(tempFileName + "-Stats.log");
StringBuilder sb = new StringBuilder();
sb.Append("Division");
sb.Append("\t");
sb.Append("Zip Files");
sb.Append("\t");
sb.Append("Conversions");
sb.Append("\t");
sb.Append("Returned Files");
sb.Append("\t");
sb.Append("Total E-Mails");
sb.Append("\t");
}
else
{
dailyStats = File.AppendText(tempFileName + "-Stats.log");
}
if (writeLog)
{
//Use a string builder to assemble the content for performance reasons
StringBuilder s = new StringBuilder();
s.Append(division);
s.Append("\t");
s.Append(zipFiles);
s.Append("\t");
s.Append(conversions);
s.Append("\t");
s.Append(returnedFiles);
s.Append("\t");
s.Append(totalEmails);
s.Append("\t");
dailyStats.WriteLine(s.ToString());
}
dailyStats.Close();
}
Are you not missing code in the first block?:
dailyStats.WriteLine(sb.ToString());
Thus:
if (File.Exists(tempFileName))
{
dailyStats = new StreamWriter(tempFileName + "-Stats.log");
StringBuilder sb = new StringBuilder();
sb.Append("Division");
sb.Append("\t");
sb.Append("Zip Files");
sb.Append("\t");
sb.Append("Conversions");
sb.Append("\t");
sb.Append("Returned Files");
sb.Append("\t");
sb.Append("Total E-Mails");
sb.Append("\t");
// Add this ......
dailyStats.WriteLine(sb.ToString());
}
You can fix it like this
var sb = new StringBuilder();
string logFileName = tempFileName + "-Stats.log";
if (File.Exists(logFileName)) {
dailyStats = File.AppendText(logFileName);
} else {
dailyStats = new StreamWriter(logFileName);
// Write header
sb.Append("Division");
...
sb.AppendLine();
}
if (writeLog) {
sb.Append(division);
...
dailyStats.WriteLine(sb.ToString());
}
dailyStats.Close();
UPDATE
The code had different errors. Two StringBuilders were created, but only one was written to the file. The existence of the file was determined for a different file name than the actual file that was written to. And finally, the logic depending on the existence of the file was inverted. I rewrote and refactored the code completely, in order to make it more understandable and manageable
public static void Open(string tempFileName, string division,
int zipFiles, int conversions, int returnedFiles, int totalEmails)
{
if (!writeLog)
return;
using (StreamWriter dailyStats = OpenLogFile(tempFileName)) {
var sb = new StringBuilder();
sb.Append(division);
// ...
dailyStats.WriteLine(sb.ToString());
}
}
private static StreamWriter OpenLogFile(string tempFileName)
{
StreamWriter dailyStats;
string logFileName = tempFileName + "-Stats.log";
if (File.Exists(logFileName)) {
dailyStats = File.AppendText(logFileName);
} else {
dailyStats = new StreamWriter(logFileName);
WriteHeader(dailyStats);
}
return dailyStats;
}
private static void WriteHeader(StreamWriter dailyStats)
{
var sb = new StringBuilder();
sb.Append("Division");
// ...
dailyStats.WriteLine(sb.ToString());
}
Note: The using statement closes the file and releases the external resources automatically.
You're creating a second StringBuilder and not doing anything with it. You probably want to just define the StringBuilder at a higher level so that appending to it in either block all adds it to one SB which can be written out at the end.
The other option of course is to write out the contents of the StringBuilder used to write the header to dailyStats rather than just doing nothing with it after appending the strings.