日期:2014-05-17  浏览次数:20822 次

为什么在winxp下可以运行的代码在win7下就报错
在winxp下可以运行的代码在win7下就报错,错误提示“文件格式无效”
代码如下:
C# code

public string ReadFile()
        {
            try
            {
                StreamReader sr = new StreamReader(fileName, Encoding.GetEncoding("gb2312"));
                string result = sr.ReadToEnd();
                sr.Close();
                return result;
            }
            catch
            { return "∷"; }
            //return null;
        }



------解决方案--------------------
权限和路径问题。
------解决方案--------------------
public string ReadFile()
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string result = sr.ReadToEnd();
sr.Close();
return result;
}
catch
{ return "∷"; }
//return null;
}
------解决方案--------------------
win7用管理员帐号进去以管理员身份运行试试

app.manifest
里写入
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the 
requestedExecutionLevel node with one of the following.

<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

If you want to utilize File and Registry Virtualization for backward 
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
可判断是否有管理员权限
------解决方案--------------------
System.IO.File.ReadAllText(),试试这个.