简体   繁体   English

与C#App共享C ++ CFile不能正常工作吗?

[英]C++ CFile sharing with C# App not working?

I'm having trouble getting a file created and populated with a c++ application to be read by a different c# application while it's still open in the c++ application. 我在创建文件并使用c ++应用程序填充文件时遇到麻烦,而该文件仍可以在c ++应用程序中打开,而由其他c#应用程序读取。

I create the file with the line: 我用以下行创建文件:

txtFile.Open(m_FileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite, &e)

I've also tried using the lines: 我也尝试过使用以下行:

txtFile.Open(m_FileName, CFile::modeCreate|CFile::modeWrite|CFile::shareDenyNone, &e)

and: 和:

txtFile.Open(m_FileName, CFile::modeCreate|CFile::modeWrite, &e)

with the same results. 具有相同的结果。

Then in the c# app I've tried 2 different ways of opening the file: 然后,在c#应用程序中,我尝试了2种打开文件的方法:

FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();

and

byte[] buffer;
using (FileStream stream = new FileStream(inputfilepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
}

Both methods catch the error: 两种方法都会捕获错误:

The process cannot access the file 'filename.txt' because it is being used by another process. 该进程无法访问文件“ filename.txt”,因为它正在被另一个进程使用。

The c++ app creates the file, then uses CreateProcess to run the c# app. C ++应用程序创建文件,然后使用CreateProcess运行C#应用程序。

I expect the issue to be in the c# code where I try read the file as notepad gives no errors when I add share permissions in the c++ app, but does give errors when I don't set permissions. 我希望问题出在C#代码中,因为我在c ++应用程序中添加共享权限时,记事本不会出现任何错误,而当我未设置权限时却会出错,因此我尝试读取该文件。

In the end I got it to work the way I wanted with Soonts' suggestion. 最后,我按照Soonts的建议以自己想要的方式工作。

Setting the c++ fileshare option to: 将c ++ fileshare选项设置为:

txtFile.Open(m_FileName, CFile::modeCreate|CFile::modeWrite|CFile::shareDenyWrite, &e)

Lets the c# application read the file, but not write to it. 让C#应用程序读取文件,但不写入文件。

in the c# app reading the file with: 在C#应用程序中使用以下命令读取文件:

using (FileStream stream = new FileStream(inputfilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

Sets the file access to read only. 将文件访问权限设置为只读。 But setting the file share permissions to readwrite allows the file to be opened while it's being written to by another application. 但是,将文件共享权限设置为readwrite可以在文件被另一个应用程序写入时打开。 Where I went wrong was having the fileshare permissions be read only as it conflicted with the c++ application before the file was even opened. 我出错的地方是,只有文件共享权限才可以读取,因为它甚至在打开文件之前就与c ++应用程序发生冲突。 So it wouldn't open. 因此它不会打开。

In C++, specify CFile::shareDenyNone or CFile::shareDenyWrite (like you're doing) 在C ++中,指定CFile :: shareDenyNone或CFile :: shareDenyWrite(就像您在做的那样)

In C#, specify FileShare.Write or FileShare.ReadWrite. 在C#中,指定FileShare.Write或FileShare.ReadWrite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM