保存时如何更改文件名?
how to change a file name when saving it?
我在我的代码中编辑了一个二进制文件,我想保存它。我正在使用 WriteAllBytes(string SavingPath, array) 方法。但是,我如何编码以使其具有特定名称?另外,出于好奇,C#在没有任何特定名称的情况下保存它时会给出什么名称?
谢谢
how do I code so that it has a specific name?
您将特定文件名传递给 WriteAllBytes
。例如:
File.WriteAllBytes(@"Path\To\MyFile.bin", array);
what name does C# give when saving it without any specific name?
你必须传递一个文件名,所以这是不可能的。如果您传递一个无效的文件名,该方法将抛出异常。来自 the docs:
Exceptions
ArgumentException
path
is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException
path
is null
or the byte array is empty.
PathTooLongException
The specified path, file name, or both exceed the system-defined maximum length.
DirectoryNotFoundException
The specified path is invalid (for example, it is on an unmapped drive).
命令 File.WriteAllBytes(string SavingPath, byte[ ] array) 会做你想做的。
SavingPath 可以是例如 "D:\SampleFolder\MyFile.txt"
这里,MyFile.txt为文件名。
因为我不知道你到底想完成什么,所以这只是一个想法。您必须添加一些逻辑。
必须在 SavingPath
参数中指定文件名。示例:
byte[] fileContent = ...; // your content
string filename = "test.txt";
string path = @"c:\temp";
string fullPath = Path.Combine(path, filename); // this will result in c:\temp\test.txt
File.WriteAllBytes(fullPath, fileContent);
File.WriteAllBytes(string path, byte[] bytes);
However, how do I code so that it has a specific name?
path
是文件名。
string path = @"C:Users\Name\Desktop\Filename.txt";
byte[] bytes = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
File.WriteAllBytes(path, bytes);
此代码将名为 Filename.txt
的文件保存到桌面。如果此文件已存在,其内容将被覆盖。
Also, out of curiosity, what name does C# give when saving it without any specific name?
如果您尝试在没有文件名的情况下保存,将会发生异常。
// No filename provided.
string path = @"C:Users\Name\Desktop";
byte[] bytes = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
try
{
// This will throw an exception.
File.WriteAllBytes(path, bytes);
}
catch (Exception)
{
// Error message that says its not a valid path.
Console.WriteLine(e.Message);
}
我在我的代码中编辑了一个二进制文件,我想保存它。我正在使用 WriteAllBytes(string SavingPath, array) 方法。但是,我如何编码以使其具有特定名称?另外,出于好奇,C#在没有任何特定名称的情况下保存它时会给出什么名称?
谢谢
how do I code so that it has a specific name?
您将特定文件名传递给 WriteAllBytes
。例如:
File.WriteAllBytes(@"Path\To\MyFile.bin", array);
what name does C# give when saving it without any specific name?
你必须传递一个文件名,所以这是不可能的。如果您传递一个无效的文件名,该方法将抛出异常。来自 the docs:
Exceptions
ArgumentException
path
is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException
path
isnull
or the byte array is empty.
PathTooLongException
The specified path, file name, or both exceed the system-defined maximum length.
DirectoryNotFoundException
The specified path is invalid (for example, it is on an unmapped drive).
命令 File.WriteAllBytes(string SavingPath, byte[ ] array) 会做你想做的。 SavingPath 可以是例如 "D:\SampleFolder\MyFile.txt"
这里,MyFile.txt为文件名。 因为我不知道你到底想完成什么,所以这只是一个想法。您必须添加一些逻辑。
必须在 SavingPath
参数中指定文件名。示例:
byte[] fileContent = ...; // your content
string filename = "test.txt";
string path = @"c:\temp";
string fullPath = Path.Combine(path, filename); // this will result in c:\temp\test.txt
File.WriteAllBytes(fullPath, fileContent);
File.WriteAllBytes(string path, byte[] bytes);
However, how do I code so that it has a specific name?
path
是文件名。
string path = @"C:Users\Name\Desktop\Filename.txt";
byte[] bytes = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
File.WriteAllBytes(path, bytes);
此代码将名为 Filename.txt
的文件保存到桌面。如果此文件已存在,其内容将被覆盖。
Also, out of curiosity, what name does C# give when saving it without any specific name?
如果您尝试在没有文件名的情况下保存,将会发生异常。
// No filename provided.
string path = @"C:Users\Name\Desktop";
byte[] bytes = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
try
{
// This will throw an exception.
File.WriteAllBytes(path, bytes);
}
catch (Exception)
{
// Error message that says its not a valid path.
Console.WriteLine(e.Message);
}