English 中文(简体)
如何通畅
原标题:How to get full path of StreamWriter

I m 采用相对途径创建StreamWriter。 但档案没有出现。 为了解决问题,我要核实,完全的道路是我所期望的。 因此,如果有一个<条码>StreamWriter,那么我如何能够从文件全文上找到文件。

string fileName = "relative/path.txt"
StreamWriter sw= new StreamWriter(fileName);
// What is the full path of  sw ?
最佳回答
问题回答

在我对框架的版本中,这似乎行之有效:

string fullPath = ((FileStream)(streamWriter.BaseStream)).Name;

(主编)

不是董事长。 要么以人工方式创建。 或按以下方案编制:

string fileName = @"relativepath.txt";
fileName = Path.GetFullPath(fileName);
Directory.CreateDirectory(Path.GetDirectoryName(fileName));

StreamWriter sw= new StreamWriter(fileName, true);
Dim fs As FileStream = CType(sw.BaseStream, FileStream)
Dim fi As New FileInfo(fs.Name)
Console.WriteLine(fi.FullName)

This would be better as a comment to @Jeppe Stiehl Nielsen s reply, but I can t add comments: In VB.Net, this becomes:

<编码> 页: 1 Path As String = CType (streamWriter). BaseStream, fileStream。 姓名:<0>

我不敢肯定为什么需要<代码>CType,而只是为了这一财产,而所有其他财产都不是,而是需要。

(as <>/code>.n t 丢弃例外情况):

_logger.LogInformation("Wrote file at {0}", (streamWriter.BaseStream as FileStream)?.Name);

<代码>? 只有左边的表述结果不是null,否则,该表述才进入(评价)<代码>null。 (T)null<>/code>.

如您希望,请查看<代码>null的投影结果。 类似:

var fs = streamWriter.BaseStream as FileStream;

if (fs != null)
{
    _logger.LogInformation("Wrote file at {0}", fs.Name);
}

但是,这比你更符合需要。 用C# 7+撰写上述例子的更现代的方法是使用is的操作者:

if (streamWriter.BaseStream is FileStream fs)
{
    _logger.LogInformation("Wrote report file at {0}", fs.Name);
}

如果无法进行投射,则该日志总会带来额外好处,因为实际上<条码>f永远不会<条码>。 使用<代码>is 。 整个<代码>is 的表述评价真实或虚假,但从未评价过<代码>null。

  var fstream = sw.BaseStream as System.IO.FileStream;
  if (fstream != null)
    Console.WriteLine(GetAbsolutePathByHandle(fstream.SafeFileHandle));


[DllImport("ntdll.dll", CharSet = CharSet.Auto)]
private static extern int NtQueryObject(SafeFileHandle handle, int objectInformationClass, IntPtr buffer,  int StructSize, out int returnLength);

static string GetAbsolutePathByHandle(SafeFileHandle handle)
{
  var size = 1024;
  var buffer = Marshal.AllocCoTaskMem(size);
  try
  {
    int retSize;
    var res = NtQueryObject(handle, 1, buffer, size, out retSize);
    if (res == -1073741820)
    {
      Marshal.FreeCoTaskMem(buffer);
      size = retSize;
      Marshal.AllocCoTaskMem(size);
      res = NtQueryObject(handle, 1, buffer, size, out retSize);
    }
    if (res != 0)
      throw new Exception(res.ToString());
    var builder = new StringBuilder();
    for (int i = 4 + (Environment.Is64BitProcess ? 12 : 4); i < retSize - 2; i += 2)
    {
      builder.Append((char)Marshal.ReadInt16(buffer, i));
    }
    return builder.ToString();
  }
  finally
  {
    Marshal.FreeCoTaskMem(buffer);
  }
}

产出:

DeviceHarddiskVolume2la-bla
elativepath.txt




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签