Winform窗体程序 如何将用户设置文件放到资源文件读取和写入

Winform窗体程序 如何将用户设置文件放到资源文件读取和写入,第1张

文件作为资源文件编译然后读取还是比较容易的。但是要运行时动态读出进行修改再编译回去,那很麻烦,特别象你的要求可能是想做成单文件吧,那时程序还是载入内存的,默认情况下可能还是不能修复的。

如果想做成单文件的话,你看看一些软件虚拟工具,可以将程序打包成一个单文件进行运行的,不知能不能满足你的要求。

C#实现访问网络共享文件夹,使用 WNetAddConnection2A 和 WNetCancelConnection2A。

在目标服务器建立共享文件夹,建立访问账号test

public enum ERROR_ID

{

ERROR_SUCCESS = 0, // Success

ERROR_BUSY = 170,

ERROR_MORE_DATA = 234,

ERROR_NO_BROWSER_SERVERS_FOUND = 6118,

ERROR_INVALID_LEVEL = 124,

ERROR_ACCESS_DENIED = 5,

ERROR_INVALID_PASSWORD = 86,

ERROR_INVALID_PARAMETER = 87,

ERROR_BAD_DEV_TYPE = 66,

ERROR_NOT_ENOUGH_MEMORY = 8,

ERROR_NETWORK_BUSY = 54,

ERROR_BAD_NETPATH = 53,

ERROR_NO_NETWORK = 1222,

ERROR_INVALID_HANDLE_STATE = 1609,

ERROR_EXTENDED_ERROR = 1208,

ERROR_DEVICE_ALREADY_REMEMBERED = 1202,

ERROR_NO_NET_OR_BAD_PATH = 1203

}

public enum RESOURCE_SCOPE

{

RESOURCE_CONNECTED = 1,

RESOURCE_GLOBALNET = 2,

RESOURCE_REMEMBERED = 3,

RESOURCE_RECENT = 4,

RESOURCE_CONTEXT = 5

}

public enum RESOURCE_TYPE

{

RESOURCETYPE_ANY = 0,

RESOURCETYPE_DISK = 1,

RESOURCETYPE_PRINT = 2,

RESOURCETYPE_RESERVED = 8,

}

public enum RESOURCE_USAGE

{

RESOURCEUSAGE_CONNECTABLE = 1,

RESOURCEUSAGE_CONTAINER = 2,

RESOURCEUSAGE_NOLOCALDEVICE = 4,

RESOURCEUSAGE_SIBLING = 8,

RESOURCEUSAGE_ATTACHED = 16,

RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),

}

public enum RESOURCE_DISPLAYTYPE

{

RESOURCEDISPLAYTYPE_GENERIC = 0,

RESOURCEDISPLAYTYPE_DOMAIN = 1,

RESOURCEDISPLAYTYPE_SERVER = 2,

RESOURCEDISPLAYTYPE_SHARE = 3,

RESOURCEDISPLAYTYPE_FILE = 4,

RESOURCEDISPLAYTYPE_GROUP = 5,

RESOURCEDISPLAYTYPE_NETWORK = 6,

RESOURCEDISPLAYTYPE_ROOT = 7,

RESOURCEDISPLAYTYPE_SHAREADMIN = 8,

RESOURCEDISPLAYTYPE_DIRECTORY = 9,

RESOURCEDISPLAYTYPE_TREE = 10,

RESOURCEDISPLAYTYPE_NDSCONTAINER = 11

}

[StructLayout(LayoutKind.Sequential)]

public struct NETRESOURCE

{

public RESOURCE_SCOPE dwScope

public RESOURCE_TYPE dwType

public RESOURCE_DISPLAYTYPE dwDisplayType

public RESOURCE_USAGE dwUsage

[MarshalAs(UnmanagedType.LPStr)]

public string lpLocalName

[MarshalAs(UnmanagedType.LPStr)]

public string lpRemoteName

[MarshalAs(UnmanagedType.LPStr)]

public string lpComment

[MarshalAs(UnmanagedType.LPStr)]

public string lpProvider

}

public class NetworkConnection

{

[DllImport("mpr.dll")]

public static extern int WNetAddConnection2A (NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags)

[DllImport("mpr.dll")]

public static extern int WNetCancelConnection2A (string sharename, int dwFlags, int fForce)

public static int Connect (string remotePath, string localPath, string username, string password)

{

NETRESOURCE[] share_driver = new NETRESOURCE[1]

share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET

share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK

share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE

share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE

share_driver[0].lpLocalName = localPath

share_driver[0].lpRemoteName = remotePath

Disconnect(localPath)

int ret = WNetAddConnection2A(share_driver, password, username, 1)

return ret

}

public static int Disconnect (string localpath)

{

return WNetCancelConnection2A(localpath, 1, 1)

}

调用方式:

public static void TestNetWorkConnection()

{

string localpath = "X:"

//int status = NetworkConnection.Connect(@"\\192.168.0.2\test", localpath, @"test", "test")

int status = NetworkConnection.Connect("\\\\192.168.0.2\\test", localpath, @"test", "test")

if (status == (int)ERROR_ID.ERROR_SUCCESS)

{

FileStream fs = new FileStream(localpath + @"\\123.txt", FileMode.OpenOrCreate)

using (StreamWriter stream = new StreamWriter(fs))

{

stream.WriteLine("你好呀,成功了")

stream.Flush()

stream.Close()

}

fs.Close()

}

else

{

Console.WriteLine(status)

}

NetworkConnection.Disconnect(localpath)

}

下载的时候请是用文件流··

如果服务器设置了关闭ping的方法。

就很难实现了·

参考:

http://support.microsoft.com/kb/173011

http://support.microsoft.com/kb/842789

你是想读取独立存储器中的数据吗?

以下是读取独立存储器的方法:

1:决定需要使用哪种范围的独立存储器。

2:调用IsolateSotrageFile类对应的方法返回独立存储器实例。

例如:IsolateSotrageFileisoFile = IsolateStorageFile.GetUserStoreForDomain()

3:使用搜索的方法GetFileNames()判断对应的文件是否在独立存储器中存在。

4:如果文件存在则根据对应的文件名创建IsolateStorageFileStream对象

创建IsolateStorageFileStream对象如下:

IsolateStorageFileStreamisoStream = new IsolateStorageFileStream(“test.txt”, FileMode.Open,isoFile)

第三个参数为IsolateStorageFIle实例,该参数告诉IsolateStorageFileStream对象从指定的独立存储器中创建流。

5:使用以上步骤中创建好的isoStream来创建StreamReader。

例如:

StreamReadersr = new StreamReader(isoStream)

6:创建好StreamReader对象后就可以像读取普通文件那样读取独立存储器中的文件了。

7:读取完成后需要关闭以下对象来释放资源,使用对应的close()方法。StreamReader,IsolateStorageFileStream,IsolateStoreFile。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11850694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存