标签: filestream

FileStream 与 StreamReader 和 StreamWriter - 有什么区别?

当处理无法通过 完成的文件时StreamReader,通过 允许哪些功能,反之亦然?我检查了文档,它们都有读/写选项,包括更高级的选项。那么我什么时候应该使用它们呢?StreamWriterFileStream

c# file filestream streamwriter streamreader

1
推荐指数
2
解决办法
5008
查看次数

如何获取受管System.IO.FileStream的基础stdio FILE*?

我正在为C/C++库编写.NET适配器,其中方法"bar"采用常规stdio FILE*.是否可以构建一个接口,以便托管代码用户可以传递托管(文件)流?那就是没有创建中间缓冲区和代码来管理数据.还假设bar()读取只会让事情变得更好吗?

// native code
void bar(FILE*);

// interface for managed code
void foo(System::IO::FileStream^ file)
{
    FILE* stdio_handle = ???;

    bar(stdio_handle);
}
Run Code Online (Sandbox Code Playgroud)

.net c++ stdio filestream

0
推荐指数
1
解决办法
281
查看次数

AS3/Air:PNG> File> FileStream> ByteArray> BitmapData

我想使用FileSteam.open()从磁盘同步读取图像文件.然后我可以使用readBytes()将它们放入ByteArray中,但是我找不到如何将它转换为BitmapData.我知道Image可以按原样读取,但我需要BitmapData.

有什么建议?

air filestream actionscript-3 bitmapdata

0
推荐指数
1
解决办法
8246
查看次数

如何在C#中将XDocument转换为FileStream

我已经看过这篇文章如何正确打开FileStream以供XDocument使用,但它没有帮助我,因为我没有本地硬盘驱动器上的文件的路径.这个XDocument正在传递给另一台计算机,因此BaseURI毫无价值.

我想知道如何在不使用本地文件路径的情况下将XDocument转换为FileStream.

c# sharepoint linq-to-xml filestream

0
推荐指数
1
解决办法
1977
查看次数

C++打开文件流

所以我基本上需要我的程序来打开文件并做一些事情.当程序要求用户输入文件名,并且用户第一次正确输入文件名时,操作正常.但是如果用户输入错误的名称,程序会说"无效名称再次尝试",但即使用户正确键入名称,它也永远无法打开文件.这是代码:

ifstream read_file(file.c_str());
while(true)
{
    if(!(read_file.fail()))
    { 
        ...
    }
    else 
    {
        cout << "Either the file doesn't exist or the formatting of the file is incorrect. Try again.\n>";
    }
    cin >> file;
    ifstream read_file(file.c_str());
}
Run Code Online (Sandbox Code Playgroud)

有什么问题,有什么想法吗?谢谢

c++ file-io filestream

0
推荐指数
1
解决办法
561
查看次数

C#文件在制作后被锁定

我正在制作一个Windows服务,部分使用FileStream和StreamWriter创建一个.eml/txt文件,它完美地工作,做我需要做的一切.唯一的问题是它所做的新文件由于某种原因后来仍被服务使用,我不知道为什么.任何线索?修复?

我认为它必须是与FileStream和StreamWriter相关的线条,因为它们是触及新文件的唯一东西.提前致谢!

Service1.cs

 public partial class emlService : ServiceBase
{
    Boolean _isRunning;

    public emlService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("emlServiceSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "emlServiceSource", "emlServiceLog");
        }
        eventLog1.Source = "emlSerivceSource";
        eventLog1.Log = "emlServiceLog";
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
        Thread NewThread = new Thread(new ThreadStart(runProc));
        _isRunning = true;
        //Creates bool to start thread loop
        if (_isRunning)
        {
            NewThread.Start();
        }
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("In OnStop");
        _isRunning = false;             
    }

    protected override void OnContinue()
    {

    }

    public void runProc()
    { …
Run Code Online (Sandbox Code Playgroud)

c# file filestream streamwriter filelock

0
推荐指数
2
解决办法
238
查看次数

不会写入文本文件

我已经创建了一个表单.如果接收到所有信息并且似乎工作,但是当我检查文本文件时没有写入任何内容,并且它只允许我在错误输出之前运行表单两次.有谁看到了问题?

const string FileName = "Friends.txt";
Friend friend = new Friend();
FileStream file = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
FileStream file2 = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 

public Form1()
{
    InitializeComponent();
}

private void enter_Click(object sender, EventArgs e)
{
    StreamWriter write = new StreamWriter(file2);

    try
    {
        friend.FirstName = firstName.Text;
        friend.LastName = lastName.Text;
        friend.PhoneNumber = phoneNumber.Text;
        friend.Month = Convert.ToInt32(birthMonth.Text);
        friend.Day = Convert.ToInt32(birthday.Text);
        write.WriteLine(friend.ToString());
        MessageBox.Show("Wrote " + friend.ToString() + " to file.");
    }
    catch(Exception error)
    {
        MessageBox.Show(error.Message + " Please reenter the …
Run Code Online (Sandbox Code Playgroud)

c# file filestream streamwriter winforms

0
推荐指数
1
解决办法
609
查看次数

我应该解除随机异步工作代码吗?

我有一些有时可以工作的异步代码,有时却没有.使用此代码,始终会创建文件,但有时它是空的(我总是发送包含内容的文件):

[HttpPost]
[Route("api/inventory/sendxml/{userId}/{pwd}/{filename}")]
public async void SendInventoryXML(String userId, String pwd, String fileName)
{
    Task task = Request.Content.ReadAsStreamAsync().ContinueWith(t =>
    {
        var stream = t.Result;
        using (FileStream fileStream = File.Create(String.Format(@"C:\HDP\{0}.xml", fileName), (int)stream.Length))
        {
            byte[] bytesInStream = new byte[stream.Length];
            stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

那么,我是否应该取消同步这个随机工作的代码,如果是的话,如何(如果没有不同步的变幻无常的事情(假设是导致这种随机行为的问题)将会完成同样的事情)?

c# http-post filestream task-parallel-library async-await

0
推荐指数
1
解决办法
68
查看次数

使用FileStream写入文件而不使用"使用"

我有一个类成员变量_stream,它是一个FileStream.这个变量在构造函数中初始化,需要坚持到我完成这个类.我有另一个类,每次需要写入文件时都会引发一个事件.在我的主类中,我有一个执行实际写作的事件处理程序.我在这个问题中尝试了以下代码:

void MyEventHandler(string message)
{
    using (_stream)
        using (StreamWriter s = new StreamWriter(_stream))
           s.WriteLine(message);
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为在我完成它之前using处置了我FileStream.我应该做什么呢?

c# file-io events using filestream

0
推荐指数
1
解决办法
1069
查看次数

如何读取包含多行记录的文件 - C#

我有这个只有一行的文本文件.每个文件包含一个客户名称,但包含多个项目和描述.
以00(公司名称)开头的记录的字符长度为10
01(项目编号) - 字符长度为10
02(描述) - 字符长度为50

我知道如何读取文件,但我不知道如何只循环一行,查找记录00,01,02并根据长度抓取文本,最后从最后记录的位置开始并再次启动循环.有人可以告诉我如何读取这样的文件吗?

输出:

companyName     16622        Description
companyName     15522        Description
Run Code Online (Sandbox Code Playgroud)

输入文本文件示例

00Init    0115522   02Description                                     0116622   02Description                                    
Run Code Online (Sandbox Code Playgroud)

c# asp.net filestream

0
推荐指数
1
解决办法
183
查看次数