FileStream与变量连接时的非法字符

huangapple go评论72阅读模式
英文:

Illegal Characters in FileStream when concatenated with variables

问题

我在尝试使用FileStream创建文件时遇到了问题,由于某种原因,当变量time以某种方式接收数据时,一切都正常,但如果我关闭程序然后再次打开它,并且该变量来自csv文件,我会得到这个错误:“System.ArgumentException: '路径中包含非法字符。' 我在pathSave变量上进行了MessageBox.Show,结果是正确的 "C:\Users\Felipe\AppData\Local\Temp\diskSTLQUZFTOV.vhd",但我不知道为什么通过csv的方式显示不是这样。

private void FileEncrypt(string outFile, string password, string time)
{
    byte[] salt = GenerateRandomSalt();
    FileStream fsCrypt = new FileStream(outFile, FileMode.Create);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
    RijndaelManaged AES = new RijndaelManaged();
    AES.KeySize = 256;
    AES.BlockSize = 128;
    AES.Padding = PaddingMode.PKCS7;
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
    AES.Key = key.GetBytes(AES.KeySize / 8);
    AES.IV = key.GetBytes(AES.BlockSize / 8);
    AES.Mode = CipherMode.CFB;
    fsCrypt.Write(salt, 0, salt.Length);
    CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
    string pathSave = Path.GetTempPath() + "disk" + time + ".vhd";
    MessageBox.Show(pathSave);
    FileStream fsIn = new FileStream(pathSave, FileMode.Open);
    byte[] buffer = new byte[1048576];
    int read;
    try
    {
        while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
        {
            Application.DoEvents();
            cs.Write(buffer, 0, read);
        }
        fsIn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message, "Unmount Encryption - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        cs.Close();
        fsCrypt.Close();
    }
}
英文:

I have a problem trying to create a file via FileStream, for some reason when the variable time receives the data in a way everything works fine, but if I close the program and open it again and that variable comes from a csv file I get this error : "System.ArgumentException: 'Illegal characters in path.' I did a MessageBox.Show on the pathSave variable and the result is correct "C:\Users\Felipe\AppData\Local\Temp\diskSTLQUZFTOV.vhd" I don't know why it's not showing this way via csv.

private void FileEncrypt(string outFile, string password, string time)
        {
            byte[] salt = GenerateRandomSalt();
            FileStream fsCrypt = new FileStream(outFile, FileMode.Create);
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            RijndaelManaged AES = new RijndaelManaged();
            AES.KeySize = 256;
            AES.BlockSize = 128;
            AES.Padding = PaddingMode.PKCS7;
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);
            AES.Mode = CipherMode.CFB;
            fsCrypt.Write(salt, 0, salt.Length);
            CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
            string pathSave = Path.GetTempPath() + "disk" + time + ".vhd";
            MessageBox.Show(pathSave);
            FileStream fsIn = new FileStream(pathSave, FileMode.Open);
            byte[] buffer = new byte[1048576];
            int read;
            try
            {
                while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Application.DoEvents();
                    cs.Write(buffer, 0, read);
                }
                fsIn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Unmount Encryption - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                cs.Close();
                fsCrypt.Close();
            }
        }

答案1

得分: 1

你可以在保存之前简单地替换路径中的任何无效字符,如下所示:

    char[] invalidChars = Path.GetInvalidFileNameChars();
    string correctedFormatTime = time;
    foreach (char c in invalidChars)
    {
        correctedFormatTime = correctedFormatTime.Replace(c.ToString(),"");
    }
    string pathSave = Path.GetTempPath() + "disk" + sanitizedTime + ".vhd";
英文:

You can simply replace any invalid characters from the path before saving like this:

char[] invalidChars = Path.GetInvalidFileNameChars();
string correctedFormatTime = time;
foreach (char c in invalidChars)
{
    correctedFormatTime = correctedFormatTime.Replace(c.ToString(),"");
}
string pathSave = Path.GetTempPath() + "disk" + sanitizedTime + ".vhd";

huangapple
  • 本文由 发表于 2023年1月9日 04:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定