标题说明了一切:
我可以确认这两个文件大小相同(以下方法返回true)但我无法再提取副本版本.
我错过了什么吗?
Boolean MyMethod(){
using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) {
String AsString = sr.ReadToEnd();
byte[] AsBytes = new byte[AsString.Length];
Buffer.BlockCopy(AsString.ToCharArray(), 0, AsBytes, 0, AsBytes.Length);
String AsBase64String = Convert.ToBase64String(AsBytes);
byte[] tempBytes = Convert.FromBase64String(AsBase64String);
File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
}
FileInfo orig = new FileInfo("C:\...\file.tar.gz");
FileInfo copy = new FileInfo("C:\...\file_copy.tar.gz");
// Confirm that both original and copy file have the same number of bytes
return (orig.Length) == (copy.Length);
}
Run Code Online (Sandbox Code Playgroud)
编辑:工作示例更简单(感谢@TS):
Boolean MyMethod(){
byte[] AsBytes = File.ReadAllBytes(@"C:\...\file.tar.gz");
String AsBase64String …Run Code Online (Sandbox Code Playgroud) 我现在开始使用PowerShell并且经过大量时间使用Unix shell并想知道如何检查文件或目录的存在.
在Powershell中,为什么Exist在下面的表达式中返回false?
PS H:\> ([System.IO.FileInfo]"C:\").Exists
False
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来检查文件是否是一个目录:
PS H:\> ([System.IO.FileInfo]"C:\").Mode.StartsWith("d")
True
Run Code Online (Sandbox Code Playgroud) 我想知道是否有人可以帮助我理解为什么System.IO.FileInfo在处理相对路径时 Windows 上的行为与 Linux 上的行为不同。
PS /home/user/Documents> ([System.IO.FileInfo]'./test.txt').FullName
/home/user/Documents/test.txt
Run Code Online (Sandbox Code Playgroud)
PS C:\Users\User\Documents> ([System.IO.FileInfo]'.\test.txt').FullName
C:\Users\User\test.txt
Run Code Online (Sandbox Code Playgroud)
System.IO.FileInfo为了澄清上述内容, Windows 或 Linux 上处理相对路径的方式没有区别。该问题与或 未[System.IO.Directory]::GetCurrentDirectory()更新有关。Push-LocationSet-Location
一个简单的例子:
PS /home/user> [System.IO.Directory]::GetCurrentDirectory()
/home/user
PS /home/user> cd ./Documents/
PS /home/user/Documents> [System.IO.Directory]::GetCurrentDirectory()
/home/user
Run Code Online (Sandbox Code Playgroud)
假设这是预期的行为,那么处理param(...)脚本和函数上的块以接受两种情况(绝对和相对)的最佳方法是什么。我曾经输入约束路径参数,System.IO.FileInfo但现在我可以看到它显然是错误的。
这是我遇到的情况,但我想知道是否有更好的方法。
我相信Split-Path -IsAbsolute使用网络路径也会带来问题,如果我错了,请纠正我。
param(
[ValidateScript({
if(Test-Path $_ -PathType Leaf) {
return $true
}
throw 'Invalid File Path'
})]
[string] $Path
)
if(-not (Split-Path $Path -IsAbsolute)) { …Run Code Online (Sandbox Code Playgroud) 我假设.NET DirectoryInfo和FileInfo对象类似于Java的java.io.File,即它们代表抽象路径,并不一定连接到现有的物理路径.
我可以用不同的方式做我正在尝试做的事情(清空文件夹并创建它,如果它不存在),但是我想知道为什么不这样做:
using System.IO;
namespace TestWipeFolder
{
internal class Program
{
private static void Main(string[] args)
{
var di = new DirectoryInfo(@"C:\foo\bar\baz");
if (di.Exists)
{
di.Delete(true);
}
// This doesn't work. C:\foo\bar is still there but it doesn't remake baz.
di.Create();
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我重启后尝试了相同的代码,它工作正常.我仍然想知道Java File对象有什么相似之处,以及删除DirectoryInfo对象引用的文件夹是否可以搞砸了,但现在却处于后台.
如何在Powershell中检索Windows 8.1特殊类型的OneDrive文件,仅限在线文件的文件信息?
虽然Windows资源管理器为OneDrive上的所有文件提供典型的文件信息(如修改日期,文件类型和大小),但Powershell的Get-ChildItem cmdlet仅"看到"可脱机使用的OneDrive文件.
我有以下代码:
foreach (string p in dirs)
{
string path = p;
string lastAccessTime = File.GetLastAccessTime(path).ToString();
bool DirFile = File.Exists(path);
FileInfo fInf = new FileInfo(path);
DateTime lastWriteTime = File.GetLastWriteTime(p);
dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + fInf.Length.ToString());
}
Run Code Online (Sandbox Code Playgroud)
我有一个fInf.Length.ToString(),我想用kbs来衡量输出.有关如何实现这一目标的任何想法?例如,我不想将2048作为文件大小,而是想得到2Kb.
在此先感谢您的帮助
我想知道如何使用里面的文件返回我的文件夹列表.
因为我不能返回两个列表...并且不能将两种类型都放在列表中.
public List<DirectoryInfo> GetTopFolders()
{
List<DirectoryInfo> Folders = new List<DirectoryInfo>();
List<FileInfo> Files = new List<FileInfo>();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C://inetpub//wwwroot//Files//");
di.GetDirectories();
System.IO.FileInfo[] fiArr = di.GetFiles();
foreach (DirectoryInfo t in di.GetDirectories())
{
Folders.Add(t);
foreach (FileInfo f in di.GetFiles())
{
Files.Add(f);
}
}
return Folders;
}
Run Code Online (Sandbox Code Playgroud)
这是我实际使用的代码.
以下System.IO.FileInfo属性是否映射到Windows资源管理器应用程序中的以下属性:
Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
Dim diar1 As IO.FileInfo() = FolderName.GetFiles("*.xml")
Run Code Online (Sandbox Code Playgroud)
我正在通过diar1循环,如下所示
For Each CurrFile In diar1
Next
Run Code Online (Sandbox Code Playgroud)
这会锁定该目录中的所有文件吗?
如果是这样,我完成后如何处理它?任何帮助,将不胜感激.
这是我的其余代码:
Public Sub Get_FTP_video(ByVal curr As Integer)
Try
Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
Dim diar1 As IO.FileInfo() = FolderName.GetFiles("*.xml")
Dim CurrFile As IO.FileInfo
Dim CurrfileName As String
Dim currsize, MinSize As Integer
currsize = 0
MinSize = 0
For Each CurrFile In diar1
CurrfileName = CurrFile.Name
If CurrFile.CreationTime.Date = Today.Date Then
GET_FileName(FileLocationFolder & CurrfileName, currsize)
If MinSize = 0 Then …Run Code Online (Sandbox Code Playgroud)