如何将计算结果保存到C#中的文件中?

时间:2017-09-10 14:00:15

标签: c#

我想学习制作一个计算机MD5哈希的程序,使其十六进制数字像pi的十进制数字一样开始。我试着用C#做。但是,如何解决以下代码崩溃到错误的问题?我想我需要先关闭一些流程然后打开另一个流程,但我该怎么做呢?

System.IO.IOException: 'The process cannot access the file 'C:\Users\User\Desktop\last.txt' because it is being used by another process.'

这是代码

//Title of this code
//Rextester.Program.Main is the entry point for your code. Don't change it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;

using System.Text;
using System.Security.Cryptography;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string a = "a";
            string path = @"C:\Users\Uname\Desktop\test.txt";

            string text;
            var fileStream = new FileStream(@"C:\Users\Uname\Desktop\test.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                text = streamReader.ReadToEnd();
            }
            Console.ReadKey();
            do
            {
                while (!Console.KeyAvailable)
                {
                    //                   Console.WriteLine("md5("+a+")="+CalculateMD5Hash(a));

                    if (CommonPrefix(CalculateMD5Hash(a), "314159265353") >= 3)
                    {
                        Console.WriteLine(a + " " + CalculateMD5Hash(a));
                        File.WriteAllText(path, a + " " + CalculateMD5Hash(a));
                    }
                    a = Next(a);
                }

            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
            //System.IO.File.WriteAllText("c:\\test.txt", a);
            var fileStream2 = new FileStream(@"C:\Users\Uname\Desktop\last.txt", FileMode.Open, FileAccess.Read);
            File.AppendAllText(@"C:\Users\Uname\Desktop\last.txt", a);

        }
        public static int CommonPrefix(String a, String b)
        {
            int i = 0;
            while (a.Substring(i, 1) == b.Substring(i, 1))
            {
                ++i;
            }
            return i;
        }

        public static string CalculateMD5Hash(string input)
        {
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                byte[] retval = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
                StringBuilder sb = new StringBuilder();
                for (int i=0;i<retval.Length;++i)
                {
                    sb.Append(retval[i].ToString("x2"));
                }
                return sb.ToString();
            }

        }

        public static string Next(string str)
        {
            string result = String.Empty;
            int index = str.Length - 1;
            bool carry;
            do
            {
                result = Increment(str[index--], out carry) + result;
            }
            while (carry && index >= 0);
            if (index >= 0) result = str.Substring(0, index + 1) + result;
            if (carry) result = "a" + result;
            return result;
        }

        private static char Increment(char value, out bool carry)
        {
            carry = false;
            if (value >= 'a' && value < 'z')
            {
                return (char)((int)value + 1);
            }
            if (value == 'z')
            {
                carry = true;
                return 'a';
            }
            throw new Exception(String.Format("Invalid character value: {0}", value));
        }

    }
}

0 个答案:

没有答案