简体   繁体   English

通过P / Invoke在ASP.NET bin文件夹中读取/写入文件时出现问题

[英]Issue in Reading/Writing files through P/Invoke in ASP.NET bin folder

I have been trying to write and read files through P/Invoke via my ASP.NET website. 我一直在尝试通过ASP.NET网站通过P / Invoke写入和读取文件。 I am facing a problem as to where the files are written/read when doing this through dlls in a website. 我在通过网站中的dlls进行写入/读取文件时遇到问题。 I have tried to explain the problem with the below example: 我试图用下面的例子来解释这个问题:

.cpp file (containing a read and write function) .cpp文件(包含读写功能)

extern "C" TEST_API int fnTest(char* fileDir)
{
ofstream myfile;
myfile.open (strcat(fileDir, "test.txt"));
myfile << "Writing this to a file.\n";
myfile.close();
}

extern "C" TEST_API char* fnTest1(char* fileDir)
{
ifstream myReadFile;
myReadFile.open(strcat(fileDir, "test1.txt"));
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
    myReadFile >> output;
return output;
}

Post build event of website to copy the dll's from above C++ project to website's bin folder 发布网站的构建事件以将dll从上述C ++项目复制到网站的bin文件夹中

Default.aspx.cs - C# Default.aspx.cs-C#
Dll functions DLL功能

public static class Functions(){
DllImport[("Test1.dll", EntryPoint="fnTest", CharSet=CharSet.Ansi]
public static extern int fnTest(string dir);

DllImport[("Test1.dll", EntryPoint="fnTest1", CharSet=CharSet.Ansi]
public static extern StringBuilder fnTest1(string dir);
}

Page_Load event Page_Load事件

string direc = AppDomain.CurrentDomain.BaseDirectory + "bin\\";
string txt1 = Functions.fnTest(direc).ToString(); //failing here - keeps on loading the page forever
string txt2 = Functions.fnTest(direc).ToString(); //failing here - keeps on loading the page forever

If I try the same Page_Load code in a desktop application with direc being set as current directory of the project output, everything works fine. 如果我在桌面应用程序中尝试使用相同的Page_Load代码,并将direc设置为项目输出的当前目录,则一切正常。 It's only that the directories where the files are to be written or read are kind of messed in case of web site and I am not really able to figure out how to correct this and get it working. 只是在网站的情况下,要写入或读取文件的目录有些混乱,我真的无法弄清楚如何纠正它并使它正常工作。 Suggestions would be greatly appreciated. 建议将不胜感激。

You still have a number of the same problems as you have in the last question . 您仍然遇到与上一个问题相同的许多问题

This time round your biggest problem is here: 这次您最大的问题在这里:

strcat(fileDir, "test.txt")

You can't modify fileDir since it is owned by the pinvoke marshaller. 您无法修改fileDir因为它由pinvoke编组拥有。 Instead of passing the directory to your native code, pass the full path to the file. 而不是将目录传递到您的本机代码,而是将完整路径传递到文件。 Use Path.Combine in your managed code to create that, and pass it to the native code. 在托管代码中使用Path.Combine来创建它,并将其传递给本机代码。

extern "C" TEST_API int fnTest(char* filename)
{
    ofstream myfile;
    myfile.open(filename);
    myfile << "Writing this to a file.\n";
    myfile.close();
}

and in managed code 并在托管代码中

string filename = Path.Combine(
    AppDomain.CurrentDomain.BaseDirectory, "bin", "test.txt");
string txt1 = Functions.fnTest(filename).ToString(); 

In a comment you explain that you need to concatenate strings in the native code. 在注释中,您说明您需要在本机代码中连接字符串。 You will need to create a native string to do that because you are not allowed to write to fileDir . 您将需要创建一个本机字符串来执行此操作,因为不允许您写入fileDir Something like this: 像这样:

string fileName = string(fileDir) + "test.txt";
myfile.open(fileName.c_str());

But you still need to fix fnTest1 which reads the file. 但是您仍然需要修复读取文件的fnTest1 My answer at your other question tells you how to do that. 我对其他问题的回答将告诉您如何执行此操作。

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

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