I m 采用相对途径创建StreamWriter
。 但档案没有出现。 为了解决问题,我要核实,完全的道路是我所期望的。 因此,如果有一个<条码>StreamWriter,那么我如何能够从文件全文上找到文件。
string fileName = "relative/path.txt"
StreamWriter sw= new StreamWriter(fileName);
// What is the full path of sw ?
I m 采用相对途径创建StreamWriter
。 但档案没有出现。 为了解决问题,我要核实,完全的道路是我所期望的。 因此,如果有一个<条码>StreamWriter,那么我如何能够从文件全文上找到文件。
string fileName = "relative/path.txt"
StreamWriter sw= new StreamWriter(fileName);
// What is the full path of sw ?
为了从一条相对的道路上全面走下去,使用。
例如:
string fileName = "relative/path.txt";
string fullPath = Path.GetFullPath(fileName);
在我对框架的版本中,这似乎行之有效:
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
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
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. ...
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 ...
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 ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
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, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
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. ...